Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions packages/integrations/src/extraerrordata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,43 @@ export class ExtraErrorData implements Integration {
/**
* Extract extra information from the Error object
*/
private _extractErrorData(error: ExtendedError): { [key: string]: unknown } | null {
private _extractErrorData(error: ExtendedError): Record<string, unknown> | null {
// We are trying to enhance already existing event, so no harm done if it won't succeed
try {
const nativeKeys = ['name', 'message', 'stack', 'line', 'column', 'fileName', 'lineNumber', 'columnNumber'];
const errorKeys = Object.getOwnPropertyNames(error).filter(key => nativeKeys.indexOf(key) === -1);
const nativeKeys = [
'name',
'message',
'stack',
'line',
'column',
'fileName',
'lineNumber',
'columnNumber',
'toJSON',
];

if (errorKeys.length) {
const extraErrorInfo: { [key: string]: unknown } = {};
for (const key of errorKeys) {
const value = error[key];
const extraErrorInfo: Record<string, unknown> = {};

// We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway.
for (const key of Object.keys(error)) {
if (nativeKeys.indexOf(key) !== -1) {
continue;
}
const value = error[key];
extraErrorInfo[key] = isError(value) ? (value as Error).toString() : value;
}

// Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
if (typeof error.toJSON === 'function') {
const serializedError = error.toJSON() as Record<string, unknown>;

for (const key of Object.keys(serializedError)) {
const value = serializedError[key];
extraErrorInfo[key] = isError(value) ? (value as Error).toString() : value;
}
return extraErrorInfo;
}

return extraErrorInfo;
} catch (oO) {
logger.error('Unable to extract extra data from the Error object:', oO);
}
Expand Down
66 changes: 66 additions & 0 deletions packages/integrations/test/extraerrordata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,70 @@ describe('ExtraErrorData()', () => {

expect(enhancedEvent).toEqual(event);
});

it('should call toJSON of original exception and add its properties', () => {
const error = new TypeError('foo') as ExtendedError;
error.baz = 42;
error.foo = 'bar';
error.toJSON = function() {
return {
bar: 1337,
qux: `${this.message} but nicer`,
};
};

const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
originalException: error,
});

expect(enhancedEvent.contexts).toEqual({
TypeError: {
bar: 1337,
baz: 42,
foo: 'bar',
qux: 'foo but nicer',
},
});
});

it('toJSON props should have priority over directly assigned ones', () => {
const error = new TypeError('foo') as ExtendedError;
error.baz = 42;
error.toJSON = function() {
return {
baz: 1337,
};
};

const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
originalException: error,
});

expect(enhancedEvent.contexts).toEqual({
TypeError: {
baz: 1337,
},
});
});

it('toJSON props should allow for usage of native names', () => {
const error = new TypeError('foo') as ExtendedError;
error.baz = 42;
error.toJSON = function() {
return {
message: 'bar',
};
};

const enhancedEvent = extraErrorData.enhanceEventWithErrorData(event, {
originalException: error,
});

expect(enhancedEvent.contexts).toEqual({
TypeError: {
baz: 42,
message: 'bar',
},
});
});
});