|
1 | 1 | import { ContextData } from '../ContextData'; |
2 | 2 | import { EventPluginContext } from '../EventPluginContext'; |
3 | 3 | import { IEvent } from '../../models/IEvent'; |
| 4 | +import { IError } from '../../models/IError'; |
4 | 5 | import { IErrorParser } from '../../services/IErrorParser'; |
5 | 6 |
|
6 | 7 | import { ErrorPlugin } from './ErrorPlugin'; |
7 | 8 | import { CapturedExceptions } from './ErrorPlugin-spec-exceptions'; |
8 | 9 |
|
| 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 | + |
9 | 24 | describe('ErrorPlugin', () => { |
10 | 25 |
|
11 | 26 | let target = new ErrorPlugin(); |
@@ -34,43 +49,96 @@ describe('ErrorPlugin', () => { |
34 | 49 | context = new EventPluginContext(client, event, contextData); |
35 | 50 | }); |
36 | 51 |
|
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', () => { |
38 | 59 |
|
39 | 60 | describeForCapturedExceptions((exception) => { |
40 | | - beforeEach(() => { |
41 | | - contextData.setException(exception); |
42 | | - }); |
43 | 61 |
|
44 | 62 | it('should ignore default error properties', () => { |
| 63 | + contextData.setException(exception); |
45 | 64 | 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(); |
49 | 67 | }); |
| 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'); |
50 | 79 | }); |
51 | 80 |
|
52 | 81 | 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 | + }); |
57 | 87 |
|
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); |
60 | 106 |
|
61 | 107 | target.run(context); |
62 | 108 |
|
63 | | - let error = event.data['@error']; |
64 | | - expect(error.data['@ext'].someProperty).toBe('Test'); |
| 109 | + let additionalData = getAdditionalData(event); |
| 110 | + expect(additionalData).toBeNull(); |
65 | 111 | }); |
66 | 112 | }); |
67 | 113 | }); |
68 | 114 |
|
69 | | - |
70 | 115 | function describeForCapturedExceptions(specDefinitions: (exception: any) => void) { |
71 | 116 | let keys = Object.getOwnPropertyNames(CapturedExceptions); |
72 | 117 | keys.forEach(key => { |
73 | 118 | let exception = CapturedExceptions[key]; |
74 | 119 | describe(key, () => { specDefinitions(exception); }); |
75 | 120 | }); |
76 | 121 | } |
| 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 | +} |
0 commit comments