Skip to content

Commit b633648

Browse files
committed
Support error inheritance, more unit tests
1 parent d42b526 commit b633648

8 files changed

+119
-50
lines changed

dist/exceptionless.js

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/default/ErrorPlugin-spec.ts

+84-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import { ContextData } from '../ContextData';
22
import { EventPluginContext } from '../EventPluginContext';
33
import { IEvent } from '../../models/IEvent';
4+
import { IError } from '../../models/IError';
45
import { IErrorParser } from '../../services/IErrorParser';
56

67
import { ErrorPlugin } from './ErrorPlugin';
78
import { CapturedExceptions } from './ErrorPlugin-spec-exceptions';
89

10+
11+
function BaseTestError() {
12+
this.name = 'NotImplementedError';
13+
this.someProperty = 'Test';
14+
};
15+
16+
BaseTestError.prototype = new Error();
17+
18+
function DerivedTestError() {
19+
this.someOtherProperty = 'Test2';
20+
}
21+
22+
DerivedTestError.prototype = new BaseTestError();
23+
924
describe('ErrorPlugin', () => {
1025

1126
let target = new ErrorPlugin();
@@ -34,43 +49,96 @@ describe('ErrorPlugin', () => {
3449
context = new EventPluginContext(client, event, contextData);
3550
});
3651

37-
describe('additional properties', () => {
52+
function processError(error) {
53+
let exception = throwAndCatch(error);
54+
contextData.setException(exception);
55+
target.run(context);
56+
}
57+
58+
describe('additional data', () => {
3859

3960
describeForCapturedExceptions((exception) => {
40-
beforeEach(() => {
41-
contextData.setException(exception);
42-
});
4361

4462
it('should ignore default error properties', () => {
63+
contextData.setException(exception);
4564
target.run(context);
46-
let error = event.data['@error'];
47-
expect(error).toBeDefined();
48-
expect(error.data && error.data['@ext']).toBeFalsy();
65+
let additionalData = getAdditionalData(event);
66+
expect(additionalData).toBeNull();
4967
});
68+
69+
});
70+
71+
it('should add custom properties to additional data', () => {
72+
let error = {
73+
someProperty: 'Test'
74+
};
75+
processError(error);
76+
let additionalData = getAdditionalData(event);
77+
expect(additionalData).not.toBeNull();
78+
expect(additionalData.someProperty).toBe('Test');
5079
});
5180

5281
it('should support custom exception types', () => {
53-
function NotImplementedError() {
54-
this.name = 'NotImplementedError';
55-
this.someProperty = 'Test';
56-
}
82+
processError(new BaseTestError());
83+
let additionalData = getAdditionalData(event);
84+
expect(additionalData).not.toBeNull();
85+
expect(additionalData.someProperty).toBe('Test');
86+
});
5787

58-
NotImplementedError.prototype = Error.prototype;
59-
contextData.setException(new NotImplementedError());
88+
it('should support inherited properties', () => {
89+
processError(new DerivedTestError());
90+
let additionalData = getAdditionalData(event);
91+
expect(additionalData).not.toBeNull();
92+
expect(additionalData.someProperty).toBe('Test');
93+
expect(additionalData.someOtherProperty).toBe('Test2');
94+
});
95+
96+
it('shouldn\'t set empty additional data', () => {
97+
processError({});
98+
let additionalData = getAdditionalData(event);
99+
expect(additionalData).toBeNull();
100+
});
101+
102+
it('should ignore functions', () => {
103+
let exception: any = new Error('Error with function');
104+
exception.someFunction = () => { };
105+
contextData.setException(exception);
60106

61107
target.run(context);
62108

63-
let error = event.data['@error'];
64-
expect(error.data['@ext'].someProperty).toBe('Test');
109+
let additionalData = getAdditionalData(event);
110+
expect(additionalData).toBeNull();
65111
});
66112
});
67113
});
68114

69-
70115
function describeForCapturedExceptions(specDefinitions: (exception: any) => void) {
71116
let keys = Object.getOwnPropertyNames(CapturedExceptions);
72117
keys.forEach(key => {
73118
let exception = CapturedExceptions[key];
74119
describe(key, () => { specDefinitions(exception); });
75120
});
76121
}
122+
123+
function getError(event: IEvent) {
124+
if (event && event.data && event.data['@error']) {
125+
return event.data['@error'];
126+
}
127+
return null;
128+
}
129+
130+
function getAdditionalData(event: IEvent) {
131+
let error = getError(event);
132+
if (error && error.data && error.data['@ext']) {
133+
return error.data['@ext'];
134+
}
135+
return null;
136+
}
137+
138+
function throwAndCatch(error: any): Error {
139+
try {
140+
throw error;
141+
} catch (exception) {
142+
return exception;
143+
}
144+
}

src/plugins/default/ErrorPlugin.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export class ErrorPlugin implements IEventPlugin {
1616
'line',
1717
'lineNumber',
1818
'opera#sourceloc',
19+
'sourceId',
1920
'sourceURL',
2021
'stack',
22+
'stackArray',
2123
'stacktrace'
2224
];
2325

@@ -54,22 +56,19 @@ export class ErrorPlugin implements IEventPlugin {
5456
}
5557

5658
private getAdditionalData(exception: Error): { [key: string]: any } {
57-
let keys = Object.keys(exception)
58-
.filter(key => this.ignoredProperties.indexOf(key) < 0);
59-
60-
if (keys.length === 0) {
61-
return null;
62-
}
63-
6459
let additionalData = {};
65-
66-
keys.forEach(key => {
60+
for (var key in exception) {
61+
if (this.ignoredProperties.indexOf(key) >= 0) {
62+
continue;
63+
}
6764
let value = exception[key];
6865
if (typeof value !== 'function') {
6966
additionalData[key] = value;
7067
}
71-
});
68+
}
7269

73-
return additionalData;
70+
return Object.getOwnPropertyNames(additionalData).length
71+
? additionalData
72+
: null;
7473
}
7574
}

0 commit comments

Comments
 (0)