Skip to content

Commit 6240191

Browse files
authored
feat(core): Use maxValueLength on error messages (#18301)
It can happen that error messages are too long and exceed the maximum envelope size (mentioned in #18219). `maxValueLength` now also checks for long error messages and truncates them.
1 parent 1525603 commit 6240191

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

packages/core/src/utils/prepareEvent.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,17 @@ export function applyClientOptions(event: Event, options: ClientOptions): void {
147147
}
148148

149149
const request = event.request;
150-
if (request?.url) {
151-
request.url = maxValueLength ? truncate(request.url, maxValueLength) : request.url;
150+
if (request?.url && maxValueLength) {
151+
request.url = truncate(request.url, maxValueLength);
152+
}
153+
154+
if (maxValueLength) {
155+
event.exception?.values?.forEach(exception => {
156+
if (exception.value) {
157+
// Truncates error messages
158+
exception.value = truncate(exception.value, maxValueLength);
159+
}
160+
});
152161
}
153162
}
154163

packages/core/test/lib/client.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type { ErrorEvent, Event, TransactionEvent } from '../../src/types-hoist/
2121
import type { SpanJSON } from '../../src/types-hoist/span';
2222
import * as debugLoggerModule from '../../src/utils/debug-logger';
2323
import * as miscModule from '../../src/utils/misc';
24-
import * as stringModule from '../../src/utils/string';
2524
import * as timeModule from '../../src/utils/time';
2625
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
2726
import { AdHocIntegration, AsyncTestIntegration, TestIntegration } from '../mocks/integration';
@@ -37,7 +36,6 @@ const clientProcess = vi.spyOn(TestClient.prototype as any, '_process');
3736

3837
vi.spyOn(miscModule, 'uuid4').mockImplementation(() => '12312012123120121231201212312012');
3938
vi.spyOn(debugLoggerModule, 'consoleSandbox').mockImplementation(cb => cb());
40-
vi.spyOn(stringModule, 'truncate').mockImplementation(str => str);
4139
vi.spyOn(timeModule, 'dateTimestampInSeconds').mockImplementation(() => 2020);
4240

4341
describe('Client', () => {
@@ -263,6 +261,36 @@ describe('Client', () => {
263261
);
264262
});
265263

264+
test('does not truncate exception values by default', () => {
265+
const exceptionMessageLength = 10_000;
266+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
267+
const client = new TestClient(options);
268+
269+
client.captureException(new Error('a'.repeat(exceptionMessageLength)));
270+
expect(TestClient.instance!.event).toEqual(
271+
expect.objectContaining({
272+
exception: {
273+
values: [{ type: 'Error', value: 'a'.repeat(exceptionMessageLength) }],
274+
},
275+
}),
276+
);
277+
});
278+
279+
test('truncates exception values according to `maxValueLength` option', () => {
280+
const maxValueLength = 10;
281+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, maxValueLength });
282+
const client = new TestClient(options);
283+
284+
client.captureException(new Error('a'.repeat(50)));
285+
expect(TestClient.instance!.event).toEqual(
286+
expect.objectContaining({
287+
exception: {
288+
values: [{ type: 'Error', value: `${'a'.repeat(maxValueLength)}...` }],
289+
},
290+
}),
291+
);
292+
});
293+
266294
test('sets the correct lastEventId', () => {
267295
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
268296
const client = new TestClient(options);

0 commit comments

Comments
 (0)