Skip to content

Commit 1bdace8

Browse files
committed
fix(core): Use maxValueLength in extra error data integration
1 parent 37a83ef commit 1bdace8

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

packages/core/src/integrations/extraerrordata.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Contexts, Event, EventHint, ExtendedError, IntegrationFn } from '@sentry/types';
2-
import { addNonEnumerableProperty, isError, isPlainObject, logger, normalize } from '@sentry/utils';
2+
import { addNonEnumerableProperty, isError, isPlainObject, logger, normalize, truncate } from '@sentry/utils';
33
import { defineIntegration } from '../integration';
44

55
import { DEBUG_BUILD } from '../debug-build';
@@ -27,8 +27,9 @@ const _extraErrorDataIntegration = ((options: Partial<ExtraErrorDataOptions> = {
2727
const { depth = 3, captureErrorCause = true } = options;
2828
return {
2929
name: INTEGRATION_NAME,
30-
processEvent(event, hint) {
31-
return _enhanceEventWithErrorData(event, hint, depth, captureErrorCause);
30+
processEvent(event, hint, client) {
31+
const { maxValueLength = 250 } = client.getOptions();
32+
return _enhanceEventWithErrorData(event, hint, depth, captureErrorCause, maxValueLength);
3233
},
3334
};
3435
}) satisfies IntegrationFn;
@@ -40,13 +41,14 @@ function _enhanceEventWithErrorData(
4041
hint: EventHint = {},
4142
depth: number,
4243
captureErrorCause: boolean,
44+
maxValueLength: number,
4345
): Event {
4446
if (!hint.originalException || !isError(hint.originalException)) {
4547
return event;
4648
}
4749
const exceptionName = (hint.originalException as ExtendedError).name || hint.originalException.constructor.name;
4850

49-
const errorData = _extractErrorData(hint.originalException as ExtendedError, captureErrorCause);
51+
const errorData = _extractErrorData(hint.originalException as ExtendedError, captureErrorCause, maxValueLength);
5052

5153
if (errorData) {
5254
const contexts: Contexts = {
@@ -74,7 +76,11 @@ function _enhanceEventWithErrorData(
7476
/**
7577
* Extract extra information from the Error object
7678
*/
77-
function _extractErrorData(error: ExtendedError, captureErrorCause: boolean): Record<string, unknown> | null {
79+
function _extractErrorData(
80+
error: ExtendedError,
81+
captureErrorCause: boolean,
82+
maxValueLength: number,
83+
): Record<string, unknown> | null {
7884
// We are trying to enhance already existing event, so no harm done if it won't succeed
7985
try {
8086
const nativeKeys = [
@@ -97,7 +103,7 @@ function _extractErrorData(error: ExtendedError, captureErrorCause: boolean): Re
97103
continue;
98104
}
99105
const value = error[key];
100-
extraErrorInfo[key] = isError(value) ? value.toString() : value;
106+
extraErrorInfo[key] = truncate(isError(value) ? value.toString() : value, maxValueLength);
101107
}
102108

103109
// Error.cause is a standard property that is non enumerable, we therefore need to access it separately.

packages/core/test/lib/integrations/extraerrordata.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ describe('ExtraErrorData()', () => {
3131
});
3232
});
3333

34+
it('should use maxValueLength to truncate extra data', () => {
35+
const error = new TypeError('foo') as ExtendedError;
36+
error.baz = 42;
37+
error.foo = 'a'.repeat(300);
38+
39+
const enhancedEvent = extraErrorData.processEvent?.(
40+
event,
41+
{
42+
originalException: error,
43+
},
44+
{} as any,
45+
) as SentryEvent;
46+
47+
expect(enhancedEvent.contexts).toEqual({
48+
TypeError: {
49+
baz: 42,
50+
foo: 'a'.repeat(250),
51+
},
52+
});
53+
});
54+
3455
it('doesnt choke on linked errors and stringify names instead', () => {
3556
const error = new TypeError('foo') as ExtendedError;
3657
error.cause = new SyntaxError('bar');

0 commit comments

Comments
 (0)