|
1 | 1 | import { BaseBackend } from '@sentry/core'; |
2 | 2 | import { Event, EventHint, Options, Severity, Transport } from '@sentry/types'; |
3 | | -import { |
4 | | - addExceptionTypeValue, |
5 | | - isDOMError, |
6 | | - isDOMException, |
7 | | - isError, |
8 | | - isErrorEvent, |
9 | | - isPlainObject, |
10 | | - supportsFetch, |
11 | | - SyncPromise, |
12 | | -} from '@sentry/utils'; |
| 3 | +import { addExceptionMechanism, supportsFetch, SyncPromise } from '@sentry/utils'; |
13 | 4 |
|
14 | | -import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers'; |
15 | | -import { _computeStackTrace } from './tracekit'; |
| 5 | +import { eventFromString, eventFromUnknownInput } from './eventbuilder'; |
16 | 6 | import { FetchTransport, XHRTransport } from './transports'; |
17 | 7 |
|
18 | 8 | /** |
@@ -66,96 +56,33 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> { |
66 | 56 | /** |
67 | 57 | * @inheritDoc |
68 | 58 | */ |
69 | | - public eventFromException(exception: any, hint?: EventHint): SyncPromise<Event> { |
70 | | - let event: Event; |
71 | | - |
72 | | - if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) { |
73 | | - // If it is an ErrorEvent with `error` property, extract it to get actual Error |
74 | | - const errorEvent = exception as ErrorEvent; |
75 | | - exception = errorEvent.error; // tslint:disable-line:no-parameter-reassignment |
76 | | - event = eventFromStacktrace(_computeStackTrace(exception as Error)); |
77 | | - return SyncPromise.resolve(this._buildEvent(event, hint)); |
78 | | - } |
79 | | - if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) { |
80 | | - // If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers) |
81 | | - // then we just extract the name and message, as they don't provide anything else |
82 | | - // https://developer.mozilla.org/en-US/docs/Web/API/DOMError |
83 | | - // https://developer.mozilla.org/en-US/docs/Web/API/DOMException |
84 | | - const domException = exception as DOMException; |
85 | | - const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException'); |
86 | | - const message = domException.message ? `${name}: ${domException.message}` : name; |
87 | | - |
88 | | - return this.eventFromMessage(message, Severity.Error, hint).then(messageEvent => { |
89 | | - addExceptionTypeValue(messageEvent, message); |
90 | | - return SyncPromise.resolve(this._buildEvent(messageEvent, hint)); |
91 | | - }); |
92 | | - } |
93 | | - if (isError(exception as Error)) { |
94 | | - // we have a real Error object, do nothing |
95 | | - event = eventFromStacktrace(_computeStackTrace(exception as Error)); |
96 | | - return SyncPromise.resolve(this._buildEvent(event, hint)); |
97 | | - } |
98 | | - if (isPlainObject(exception as {}) && hint && hint.syntheticException) { |
99 | | - // If it is plain Object, serialize it manually and extract options |
100 | | - // This will allow us to group events based on top-level keys |
101 | | - // which is much better than creating new group when any key/value change |
102 | | - const objectException = exception as {}; |
103 | | - event = eventFromPlainObject(objectException, hint.syntheticException); |
104 | | - addExceptionTypeValue(event, 'Custom Object', undefined, { |
105 | | - handled: true, |
106 | | - synthetic: true, |
107 | | - type: 'generic', |
108 | | - }); |
109 | | - event.level = Severity.Error; |
110 | | - return SyncPromise.resolve(this._buildEvent(event, hint)); |
111 | | - } |
112 | | - |
113 | | - // If none of previous checks were valid, then it means that |
114 | | - // it's not a DOMError/DOMException |
115 | | - // it's not a plain Object |
116 | | - // it's not a valid ErrorEvent (one with an error property) |
117 | | - // it's not an Error |
118 | | - // So bail out and capture it as a simple message: |
119 | | - const stringException = exception as string; |
120 | | - return this.eventFromMessage(stringException, undefined, hint).then(messageEvent => { |
121 | | - addExceptionTypeValue(messageEvent, `${stringException}`, undefined, { |
122 | | - handled: true, |
123 | | - synthetic: true, |
124 | | - type: 'generic', |
125 | | - }); |
126 | | - messageEvent.level = Severity.Error; |
127 | | - return SyncPromise.resolve(this._buildEvent(messageEvent, hint)); |
| 59 | + public eventFromException(exception: any, hint?: EventHint): Promise<Event> { |
| 60 | + const syntheticException = (hint && hint.syntheticException) || undefined; |
| 61 | + const event = eventFromUnknownInput(exception, syntheticException, { |
| 62 | + attachStacktrace: this._options.attachStacktrace, |
128 | 63 | }); |
| 64 | + addExceptionMechanism(event, { |
| 65 | + handled: true, |
| 66 | + type: 'generic', |
| 67 | + }); |
| 68 | + event.level = Severity.Error; |
| 69 | + if (hint && hint.event_id) { |
| 70 | + event.event_id = hint.event_id; |
| 71 | + } |
| 72 | + return SyncPromise.resolve(event); |
129 | 73 | } |
130 | | - |
131 | | - /** |
132 | | - * This is an internal helper function that creates an event. |
133 | | - */ |
134 | | - private _buildEvent(event: Event, hint?: EventHint): Event { |
135 | | - return { |
136 | | - ...event, |
137 | | - event_id: hint && hint.event_id, |
138 | | - }; |
139 | | - } |
140 | | - |
141 | 74 | /** |
142 | 75 | * @inheritDoc |
143 | 76 | */ |
144 | | - public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): SyncPromise<Event> { |
145 | | - const event: Event = { |
146 | | - event_id: hint && hint.event_id, |
147 | | - level, |
148 | | - message, |
149 | | - }; |
150 | | - |
151 | | - if (this._options.attachStacktrace && hint && hint.syntheticException) { |
152 | | - const stacktrace = _computeStackTrace(hint.syntheticException); |
153 | | - const frames = prepareFramesForEvent(stacktrace.stack); |
154 | | - event.stacktrace = { |
155 | | - frames, |
156 | | - }; |
| 77 | + public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): Promise<Event> { |
| 78 | + const syntheticException = (hint && hint.syntheticException) || undefined; |
| 79 | + const event = eventFromString(message, syntheticException, { |
| 80 | + attachStacktrace: this._options.attachStacktrace, |
| 81 | + }); |
| 82 | + event.level = level; |
| 83 | + if (hint && hint.event_id) { |
| 84 | + event.event_id = hint.event_id; |
157 | 85 | } |
158 | | - |
159 | 86 | return SyncPromise.resolve(event); |
160 | 87 | } |
161 | 88 | } |
0 commit comments