1
- import { Event , EventHint , Exception , Severity , StackFrame } from '@sentry/types' ;
1
+ import { Event , EventHint , Exception , Severity , StackFrame , StackParser } from '@sentry/types' ;
2
2
import {
3
3
addExceptionMechanism ,
4
4
addExceptionTypeValue ,
5
- createStackParser ,
6
5
extractExceptionKeysForMessage ,
7
6
isDOMError ,
8
7
isDOMException ,
@@ -14,22 +13,12 @@ import {
14
13
resolvedSyncPromise ,
15
14
} from '@sentry/utils' ;
16
15
17
- import {
18
- chromeStackParser ,
19
- geckoStackParser ,
20
- opera10StackParser ,
21
- opera11StackParser ,
22
- winjsStackParser ,
23
- } from './stack-parsers' ;
24
-
25
16
/**
26
17
* This function creates an exception from an TraceKitStackTrace
27
- * @param stacktrace TraceKitStackTrace that will be converted to an exception
28
- * @hidden
29
18
*/
30
- export function exceptionFromError ( ex : Error ) : Exception {
19
+ export function exceptionFromError ( stackParser : StackParser , ex : Error ) : Exception {
31
20
// Get the frames first since Opera can lose the stack if we touch anything else first
32
- const frames = parseStackFrames ( ex ) ;
21
+ const frames = parseStackFrames ( stackParser , ex ) ;
33
22
34
23
const exception : Exception = {
35
24
type : ex && ex . name ,
@@ -51,6 +40,7 @@ export function exceptionFromError(ex: Error): Exception {
51
40
* @hidden
52
41
*/
53
42
export function eventFromPlainObject (
43
+ stackParser : StackParser ,
54
44
exception : Record < string , unknown > ,
55
45
syntheticException ?: Error ,
56
46
isUnhandledRejection ?: boolean ,
@@ -72,7 +62,7 @@ export function eventFromPlainObject(
72
62
} ;
73
63
74
64
if ( syntheticException ) {
75
- const frames = parseStackFrames ( syntheticException ) ;
65
+ const frames = parseStackFrames ( stackParser , syntheticException ) ;
76
66
if ( frames . length ) {
77
67
// event.exception.values[0] has been set above
78
68
( event . exception as { values : Exception [ ] } ) . values [ 0 ] . stacktrace = { frames } ;
@@ -85,16 +75,19 @@ export function eventFromPlainObject(
85
75
/**
86
76
* @hidden
87
77
*/
88
- export function eventFromError ( ex : Error ) : Event {
78
+ export function eventFromError ( stackParser : StackParser , ex : Error ) : Event {
89
79
return {
90
80
exception : {
91
- values : [ exceptionFromError ( ex ) ] ,
81
+ values : [ exceptionFromError ( stackParser , ex ) ] ,
92
82
} ,
93
83
} ;
94
84
}
95
85
96
86
/** Parses stack frames from an error */
97
- export function parseStackFrames ( ex : Error & { framesToPop ?: number ; stacktrace ?: string } ) : StackFrame [ ] {
87
+ export function parseStackFrames (
88
+ stackParser : StackParser ,
89
+ ex : Error & { framesToPop ?: number ; stacktrace ?: string } ,
90
+ ) : StackFrame [ ] {
98
91
// Access and store the stacktrace property before doing ANYTHING
99
92
// else to it because Opera is not very good at providing it
100
93
// reliably in other circumstances.
@@ -103,13 +96,7 @@ export function parseStackFrames(ex: Error & { framesToPop?: number; stacktrace?
103
96
const popSize = getPopSize ( ex ) ;
104
97
105
98
try {
106
- return createStackParser (
107
- opera10StackParser ,
108
- opera11StackParser ,
109
- chromeStackParser ,
110
- winjsStackParser ,
111
- geckoStackParser ,
112
- ) ( stacktrace , popSize ) ;
99
+ return stackParser ( stacktrace , popSize ) ;
113
100
} catch ( e ) {
114
101
// no-empty
115
102
}
@@ -155,12 +142,13 @@ function extractMessage(ex: Error & { message: { error?: Error } }): string {
155
142
* @hidden
156
143
*/
157
144
export function eventFromException (
145
+ stackParser : StackParser ,
158
146
exception : unknown ,
159
147
hint ?: EventHint ,
160
148
attachStacktrace ?: boolean ,
161
149
) : PromiseLike < Event > {
162
150
const syntheticException = ( hint && hint . syntheticException ) || undefined ;
163
- const event = eventFromUnknownInput ( exception , syntheticException , attachStacktrace ) ;
151
+ const event = eventFromUnknownInput ( stackParser , exception , syntheticException , attachStacktrace ) ;
164
152
addExceptionMechanism ( event ) ; // defaults to { type: 'generic', handled: true }
165
153
event . level = Severity . Error ;
166
154
if ( hint && hint . event_id ) {
@@ -174,13 +162,14 @@ export function eventFromException(
174
162
* @hidden
175
163
*/
176
164
export function eventFromMessage (
165
+ stackParser : StackParser ,
177
166
message : string ,
178
167
level : Severity = Severity . Info ,
179
168
hint ?: EventHint ,
180
169
attachStacktrace ?: boolean ,
181
170
) : PromiseLike < Event > {
182
171
const syntheticException = ( hint && hint . syntheticException ) || undefined ;
183
- const event = eventFromString ( message , syntheticException , attachStacktrace ) ;
172
+ const event = eventFromString ( stackParser , message , syntheticException , attachStacktrace ) ;
184
173
event . level = level ;
185
174
if ( hint && hint . event_id ) {
186
175
event . event_id = hint . event_id ;
@@ -192,6 +181,7 @@ export function eventFromMessage(
192
181
* @hidden
193
182
*/
194
183
export function eventFromUnknownInput (
184
+ stackParser : StackParser ,
195
185
exception : unknown ,
196
186
syntheticException ?: Error ,
197
187
attachStacktrace ?: boolean ,
@@ -202,7 +192,7 @@ export function eventFromUnknownInput(
202
192
if ( isErrorEvent ( exception as ErrorEvent ) && ( exception as ErrorEvent ) . error ) {
203
193
// If it is an ErrorEvent with `error` property, extract it to get actual Error
204
194
const errorEvent = exception as ErrorEvent ;
205
- return eventFromError ( errorEvent . error as Error ) ;
195
+ return eventFromError ( stackParser , errorEvent . error as Error ) ;
206
196
}
207
197
208
198
// If it is a `DOMError` (which is a legacy API, but still supported in some browsers) then we just extract the name
@@ -216,11 +206,11 @@ export function eventFromUnknownInput(
216
206
const domException = exception as DOMException ;
217
207
218
208
if ( 'stack' in ( exception as Error ) ) {
219
- event = eventFromError ( exception as Error ) ;
209
+ event = eventFromError ( stackParser , exception as Error ) ;
220
210
} else {
221
211
const name = domException . name || ( isDOMError ( domException ) ? 'DOMError' : 'DOMException' ) ;
222
212
const message = domException . message ? `${ name } : ${ domException . message } ` : name ;
223
- event = eventFromString ( message , syntheticException , attachStacktrace ) ;
213
+ event = eventFromString ( stackParser , message , syntheticException , attachStacktrace ) ;
224
214
addExceptionTypeValue ( event , message ) ;
225
215
}
226
216
if ( 'code' in domException ) {
@@ -231,14 +221,14 @@ export function eventFromUnknownInput(
231
221
}
232
222
if ( isError ( exception ) ) {
233
223
// we have a real Error object, do nothing
234
- return eventFromError ( exception ) ;
224
+ return eventFromError ( stackParser , exception ) ;
235
225
}
236
226
if ( isPlainObject ( exception ) || isEvent ( exception ) ) {
237
227
// If it's a plain object or an instance of `Event` (the built-in JS kind, not this SDK's `Event` type), serialize
238
228
// it manually. This will allow us to group events based on top-level keys which is much better than creating a new
239
229
// group on any key/value change.
240
230
const objectException = exception as Record < string , unknown > ;
241
- event = eventFromPlainObject ( objectException , syntheticException , isUnhandledRejection ) ;
231
+ event = eventFromPlainObject ( stackParser , objectException , syntheticException , isUnhandledRejection ) ;
242
232
addExceptionMechanism ( event , {
243
233
synthetic : true ,
244
234
} ) ;
@@ -254,7 +244,7 @@ export function eventFromUnknownInput(
254
244
// - a plain Object
255
245
//
256
246
// So bail out and capture it as a simple message:
257
- event = eventFromString ( exception as string , syntheticException , attachStacktrace ) ;
247
+ event = eventFromString ( stackParser , exception as string , syntheticException , attachStacktrace ) ;
258
248
addExceptionTypeValue ( event , `${ exception } ` , undefined ) ;
259
249
addExceptionMechanism ( event , {
260
250
synthetic : true ,
@@ -266,13 +256,18 @@ export function eventFromUnknownInput(
266
256
/**
267
257
* @hidden
268
258
*/
269
- export function eventFromString ( input : string , syntheticException ?: Error , attachStacktrace ?: boolean ) : Event {
259
+ export function eventFromString (
260
+ stackParser : StackParser ,
261
+ input : string ,
262
+ syntheticException ?: Error ,
263
+ attachStacktrace ?: boolean ,
264
+ ) : Event {
270
265
const event : Event = {
271
266
message : input ,
272
267
} ;
273
268
274
269
if ( attachStacktrace && syntheticException ) {
275
- const frames = parseStackFrames ( syntheticException ) ;
270
+ const frames = parseStackFrames ( stackParser , syntheticException ) ;
276
271
if ( frames . length ) {
277
272
event . exception = {
278
273
values : [ { value : input , stacktrace : { frames } } ] ,
0 commit comments