Skip to content

Commit 3c45c2f

Browse files
github-felipe-caputokamilogorek
authored andcommitted
fix: Check if value is error object in extraErrorData integration (#1817)
* check if value is error object, also use getOwnPropertyNames * use isError method
1 parent 44ce010 commit 3c45c2f

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

packages/core/src/integrations/extraerrordata.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ export class ExtraErrorData implements Integration {
2828
public setupOnce(): void {
2929
addGlobalEventProcessor(async (event: SentryEvent, hint?: SentryEventHint) => {
3030
const self = getCurrentHub().getIntegration(ExtraErrorData);
31-
32-
if (!self || !hint || !hint.originalException) {
31+
if (!self) {
3332
return event;
3433
}
35-
36-
return this.enhanceEventWithErrorData(event, hint.originalException);
34+
return self.enhanceEventWithErrorData(event, hint);
3735
});
3836
}
3937

4038
/**
4139
* Attaches extracted information from the Error object to extra field in the SentryEvent
4240
*/
43-
public enhanceEventWithErrorData(event: SentryEvent, error: Error): SentryEvent {
44-
const errorData = this.extractErrorData(error);
41+
public enhanceEventWithErrorData(event: SentryEvent, hint?: SentryEventHint): SentryEvent {
42+
if (!hint || !hint.originalException || !isError(hint.originalException)) {
43+
return event;
44+
}
45+
46+
const errorData = this.extractErrorData(hint.originalException);
4547

4648
if (errorData) {
4749
return {
@@ -64,7 +66,7 @@ export class ExtraErrorData implements Integration {
6466
try {
6567
const nativeKeys = ['name', 'message', 'stack', 'line', 'column', 'fileName', 'lineNumber', 'columnNumber'];
6668
const name = error.name || error.constructor.name;
67-
const errorKeys = Object.keys(error).filter(key => nativeKeys.indexOf(key) === -1);
69+
const errorKeys = Object.getOwnPropertyNames(error).filter(key => nativeKeys.indexOf(key) === -1);
6870

6971
if (errorKeys.length) {
7072
const extraErrorInfo: { [key: string]: unknown } = {};

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ describe('ExtraErrorData()', () => {
2121
error.baz = 42;
2222
error.foo = 'bar';
2323

24-
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, error);
24+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
25+
originalException: error,
26+
});
2527

2628
expect(enhancedEvent.extra).toEqual({
2729
TypeError: {
@@ -35,7 +37,9 @@ describe('ExtraErrorData()', () => {
3537
const error = new TypeError('foo') as ExtendedError;
3638
error.cause = new SyntaxError('bar');
3739

38-
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, error);
40+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
41+
originalException: error,
42+
});
3943

4044
expect(enhancedEvent.extra).toEqual({
4145
TypeError: {
@@ -53,7 +57,9 @@ describe('ExtraErrorData()', () => {
5357
const error = new TypeError('foo') as ExtendedError;
5458
error.baz = 42;
5559

56-
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, error);
60+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
61+
originalException: error,
62+
});
5763

5864
expect(enhancedEvent.extra).toEqual({
5965
TypeError: {
@@ -62,4 +68,28 @@ describe('ExtraErrorData()', () => {
6268
foo: 42,
6369
});
6470
});
71+
72+
it('should return event if originalException is not an Error object', () => {
73+
const error = 'error message, not object';
74+
75+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
76+
originalException: error,
77+
});
78+
79+
expect(enhancedEvent).toEqual(event);
80+
});
81+
82+
it('should return event if there is no SentryEventHint', () => {
83+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event);
84+
85+
expect(enhancedEvent).toEqual(event);
86+
});
87+
88+
it('should return event if there is no originalException', () => {
89+
const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
90+
notOriginalException: 'fooled you',
91+
});
92+
93+
expect(enhancedEvent).toEqual(event);
94+
});
6595
});

0 commit comments

Comments
 (0)