|
| 1 | +import { |
| 2 | + errorToProperties, |
| 3 | + ErrorProperties, |
| 4 | + unhandledRejectionToProperties, |
| 5 | +} from '../../../extensions/exceptions/error-conversion' |
| 6 | + |
| 7 | +// ugh, jest |
| 8 | +// can't reference PromiseRejectionEvent to construct it 🤷 |
| 9 | +export type PromiseRejectionEventTypes = 'rejectionhandled' | 'unhandledrejection' |
| 10 | + |
| 11 | +export type PromiseRejectionEventInit = { |
| 12 | + promise: Promise<any> |
| 13 | + reason: any |
| 14 | +} |
| 15 | + |
| 16 | +export class PromiseRejectionEvent extends Event { |
| 17 | + public readonly promise: Promise<any> |
| 18 | + public readonly reason: any |
| 19 | + |
| 20 | + public constructor(type: PromiseRejectionEventTypes, options: PromiseRejectionEventInit) { |
| 21 | + super(type) |
| 22 | + |
| 23 | + this.promise = options.promise |
| 24 | + this.reason = options.reason |
| 25 | + } |
| 26 | +} |
| 27 | +// ugh, jest |
| 28 | + |
| 29 | +describe('Error conversion', () => { |
| 30 | + it('should convert a string to an error', () => { |
| 31 | + const expected: ErrorProperties = { |
| 32 | + $exception_type: 'InternalError', |
| 33 | + $exception_message: 'but somehow still a string', |
| 34 | + $exception_is_synthetic: true, |
| 35 | + } |
| 36 | + expect(errorToProperties(['Uncaught exception: InternalError: but somehow still a string'])).toEqual(expected) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should convert a plain object to an error', () => { |
| 40 | + const expected: ErrorProperties = { |
| 41 | + $exception_type: 'Error', |
| 42 | + $exception_message: 'Non-Error exception captured with keys: foo, string', |
| 43 | + $exception_is_synthetic: true, |
| 44 | + } |
| 45 | + expect(errorToProperties([{ string: 'candidate', foo: 'bar' } as unknown as Event])).toEqual(expected) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should convert a plain Event to an error', () => { |
| 49 | + const expected: ErrorProperties = { |
| 50 | + $exception_type: 'MouseEvent', |
| 51 | + $exception_message: 'Non-Error exception captured with keys: isTrusted', |
| 52 | + $exception_is_synthetic: true, |
| 53 | + } |
| 54 | + const event = new MouseEvent('click', { bubbles: true, cancelable: true, composed: true }) |
| 55 | + expect(errorToProperties([event])).toEqual(expected) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should convert a plain Error to an error', () => { |
| 59 | + const error = new Error('oh no an error has happened') |
| 60 | + |
| 61 | + const errorProperties = errorToProperties(['something', undefined, undefined, undefined, error]) |
| 62 | + if (errorProperties === null) { |
| 63 | + throw new Error("this mustn't be null") |
| 64 | + } |
| 65 | + |
| 66 | + expect(Object.keys(errorProperties)).toHaveLength(3) |
| 67 | + expect(errorProperties.$exception_type).toEqual('Error') |
| 68 | + expect(errorProperties.$exception_message).toEqual('oh no an error has happened') |
| 69 | + // the stack trace changes between runs, so we just check that it's there |
| 70 | + expect(errorProperties.$exception_stack_trace_raw).toBeDefined() |
| 71 | + expect(errorProperties.$exception_stack_trace_raw).toContain('{"filename') |
| 72 | + }) |
| 73 | + |
| 74 | + class FakeDomError { |
| 75 | + constructor(public name: string, public message: string) {} |
| 76 | + [Symbol.toStringTag] = 'DOMError' |
| 77 | + } |
| 78 | + |
| 79 | + it('should convert a DOM Error to an error', () => { |
| 80 | + const expected: ErrorProperties = { |
| 81 | + $exception_type: 'Error', |
| 82 | + $exception_message: 'click: foo', |
| 83 | + } |
| 84 | + const event = new FakeDomError('click', 'foo') |
| 85 | + expect(errorToProperties([event as unknown as Event])).toEqual(expected) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should convert a DOM Exception to an error', () => { |
| 89 | + const event = new DOMException('oh no disaster', 'dom-exception') |
| 90 | + const errorProperties = errorToProperties([event as unknown as Event]) |
| 91 | + |
| 92 | + if (errorProperties === null) { |
| 93 | + throw new Error("this mustn't be null") |
| 94 | + } |
| 95 | + |
| 96 | + expect(Object.keys(errorProperties)).toHaveLength(4) |
| 97 | + expect(errorProperties.$exception_type).toEqual('dom-exception') |
| 98 | + expect(errorProperties.$exception_message).toEqual('oh no disaster') |
| 99 | + // the stack trace changes between runs, so we just check that it's there |
| 100 | + expect(errorProperties.$exception_stack_trace_raw).toBeDefined() |
| 101 | + expect(errorProperties.$exception_stack_trace_raw).toContain('{"filename') |
| 102 | + }) |
| 103 | + |
| 104 | + it('should convert an error event to an error', () => { |
| 105 | + const event = new ErrorEvent('oh no an error event', { error: new Error('the real error is hidden inside') }) |
| 106 | + |
| 107 | + const errorProperties = errorToProperties([event as unknown as Event]) |
| 108 | + if (errorProperties === null) { |
| 109 | + throw new Error("this mustn't be null") |
| 110 | + } |
| 111 | + |
| 112 | + expect(Object.keys(errorProperties)).toHaveLength(3) |
| 113 | + expect(errorProperties.$exception_type).toEqual('Error') |
| 114 | + expect(errorProperties.$exception_message).toEqual('the real error is hidden inside') |
| 115 | + // the stack trace changes between runs, so we just check that it's there |
| 116 | + expect(errorProperties.$exception_stack_trace_raw).toBeDefined() |
| 117 | + expect(errorProperties.$exception_stack_trace_raw).toContain('{"filename') |
| 118 | + }) |
| 119 | + |
| 120 | + it('can convert source, lineno, colno', () => { |
| 121 | + const expected: ErrorProperties = { |
| 122 | + $exception_colno: 200, |
| 123 | + $exception_is_synthetic: true, |
| 124 | + $exception_lineno: 12, |
| 125 | + $exception_message: 'string candidate', |
| 126 | + $exception_source: 'a source', |
| 127 | + $exception_type: 'Error', |
| 128 | + } |
| 129 | + expect(errorToProperties(['string candidate', 'a source', 12, 200])).toEqual(expected) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should convert unhandled promise rejection that the browser has messed around with', () => { |
| 133 | + const ce = new CustomEvent('unhandledrejection', { |
| 134 | + detail: { |
| 135 | + promise: {}, |
| 136 | + reason: new Error('a wrapped rejection event'), |
| 137 | + }, |
| 138 | + }) |
| 139 | + const errorProperties: ErrorProperties = unhandledRejectionToProperties([ |
| 140 | + ce as unknown as PromiseRejectionEvent, |
| 141 | + ]) |
| 142 | + expect(Object.keys(errorProperties)).toHaveLength(4) |
| 143 | + expect(errorProperties.$exception_type).toEqual('UnhandledRejection') |
| 144 | + expect(errorProperties.$exception_message).toEqual('a wrapped rejection event') |
| 145 | + expect(errorProperties.$exception_handled).toEqual(false) |
| 146 | + // the stack trace changes between runs, so we just check that it's there |
| 147 | + expect(errorProperties.$exception_stack_trace_raw).toBeDefined() |
| 148 | + expect(errorProperties.$exception_stack_trace_raw).toContain('{"filename') |
| 149 | + }) |
| 150 | + |
| 151 | + it('should convert unhandled promise rejection', () => { |
| 152 | + const pre = new PromiseRejectionEvent('unhandledrejection', { |
| 153 | + promise: Promise.resolve('wat'), |
| 154 | + reason: 'My house is on fire', |
| 155 | + }) |
| 156 | + const errorProperties: ErrorProperties = unhandledRejectionToProperties([ |
| 157 | + pre as unknown as PromiseRejectionEvent, |
| 158 | + ]) |
| 159 | + expect(Object.keys(errorProperties)).toHaveLength(3) |
| 160 | + expect(errorProperties.$exception_type).toEqual('UnhandledRejection') |
| 161 | + expect(errorProperties.$exception_message).toEqual( |
| 162 | + 'Non-Error promise rejection captured with value: My house is on fire' |
| 163 | + ) |
| 164 | + expect(errorProperties.$exception_handled).toEqual(false) |
| 165 | + }) |
| 166 | +}) |
0 commit comments