Skip to content

Commit 8990467

Browse files
committed
feat: toRoot option for ExtraErrorData integration
1 parent 01bab2d commit 8990467

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

packages/integrations/src/extraerrordata.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isError, isPlainObject, logger, normalize } from '@sentry/utils';
44
/** JSDoc */
55
interface ExtraErrorDataOptions {
66
depth?: number;
7+
toRoot?: boolean;
78
}
89

910
/** Patch toString calls to return proper name for wrapped functions */
@@ -21,7 +22,7 @@ export class ExtraErrorData implements Integration {
2122
/**
2223
* @inheritDoc
2324
*/
24-
public constructor(private readonly _options: ExtraErrorDataOptions = { depth: 3 }) {}
25+
public constructor(private readonly _options: ExtraErrorDataOptions = { depth: 3, toRoot: false }) {}
2526

2627
/**
2728
* @inheritDoc
@@ -89,9 +90,11 @@ export class ExtraErrorData implements Integration {
8990
// tslint:disable:no-unsafe-any
9091
extraErrorInfo[key] = value;
9192
}
92-
result = {
93-
[name]: extraErrorInfo,
94-
};
93+
result = this._options.toRoot
94+
? extraErrorInfo
95+
: {
96+
[name]: extraErrorInfo,
97+
};
9598
}
9699
} catch (oO) {
97100
logger.error('Unable to extract extra data from the Error object:', oO);

packages/integrations/test/extraerrordata.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ExtendedError, SentryEvent } from '@sentry/types';
33
import { ExtraErrorData } from '../src/extraerrordata';
44

55
const extraErrorData = new ExtraErrorData();
6+
const extraErrorDataWithToRoot = new ExtraErrorData({ toRoot: true });
67
let event: SentryEvent;
78

89
describe('ExtraErrorData()', () => {
@@ -63,6 +64,45 @@ describe('ExtraErrorData()', () => {
6364
});
6465
});
6566

67+
it('should append extra data to root if `toRoot` is option provided', () => {
68+
event = {
69+
extra: {
70+
foo: 42,
71+
},
72+
};
73+
const error = new TypeError('foo') as ExtendedError;
74+
error.baz = 42;
75+
76+
const enhancedEvent = extraErrorDataWithToRoot.enhanceEventWithErrorData(event, {
77+
originalException: error,
78+
});
79+
80+
expect(enhancedEvent.extra).toEqual({
81+
baz: 42,
82+
foo: 42,
83+
});
84+
});
85+
86+
it('should overwrite to root if data existing in extra field and if `toRoot` is option provided', () => {
87+
event = {
88+
extra: {
89+
foo: 40,
90+
},
91+
};
92+
const error = new TypeError('foo') as ExtendedError;
93+
error.baz = 42;
94+
error.foo = 42;
95+
96+
const enhancedEvent = extraErrorDataWithToRoot.enhanceEventWithErrorData(event, {
97+
originalException: error,
98+
});
99+
100+
expect(enhancedEvent.extra).toEqual({
101+
baz: 42,
102+
foo: 42,
103+
});
104+
});
105+
66106
it('should return event if originalException is not an Error object', () => {
67107
const error = 'error message, not object';
68108

0 commit comments

Comments
 (0)