@@ -3,6 +3,7 @@ import { SentryEvent, SentryEventHint, Severity, Transport } from '@sentry/types
3
3
import { SentryError } from '@sentry/utils/error' ;
4
4
import { isDOMError , isDOMException , isError , isErrorEvent , isPlainObject } from '@sentry/utils/is' ;
5
5
import { supportsBeacon , supportsFetch } from '@sentry/utils/supports' ;
6
+ import { SyncPromise } from '@sentry/utils/syncpromise' ;
6
7
import { addExceptionTypeValue , eventFromPlainObject , eventFromStacktrace , prepareFramesForEvent } from './parsers' ;
7
8
import { computeStackTrace } from './tracekit' ;
8
9
import { BeaconTransport , FetchTransport , XHRTransport } from './transports' ;
@@ -70,48 +71,60 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
70
71
/**
71
72
* @inheritDoc
72
73
*/
73
- public eventFromException ( exception : any , hint ?: SentryEventHint ) : SentryEvent {
74
- let event ;
75
-
76
- if ( isErrorEvent ( exception as ErrorEvent ) && ( exception as ErrorEvent ) . error ) {
77
- // If it is an ErrorEvent with `error` property, extract it to get actual Error
78
- const ex = exception as ErrorEvent ;
79
- exception = ex . error ; // tslint:disable-line:no-parameter-reassignment
80
- event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ;
81
- } else if ( isDOMError ( exception as DOMError ) || isDOMException ( exception as DOMException ) ) {
82
- // If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
83
- // then we just extract the name and message, as they don't provide anything else
84
- // https://developer.mozilla.org/en-US/docs/Web/API/DOMError
85
- // https://developer.mozilla.org/en-US/docs/Web/API/DOMException
86
- const ex = exception as DOMException ;
87
- const name = ex . name || ( isDOMError ( ex ) ? 'DOMError' : 'DOMException' ) ;
88
- const message = ex . message ? `${ name } : ${ ex . message } ` : name ;
89
-
90
- event = this . eventFromMessage ( message , undefined , hint ) ;
91
- addExceptionTypeValue ( event , message ) ;
92
- } else if ( isError ( exception as Error ) ) {
93
- // we have a real Error object, do nothing
94
- event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ;
95
- } else if ( isPlainObject ( exception as { } ) && hint && hint . syntheticException ) {
96
- // If it is plain Object, serialize it manually and extract options
97
- // This will allow us to group events based on top-level keys
98
- // which is much better than creating new group when any key/value change
99
- const ex = exception as { } ;
100
- event = eventFromPlainObject ( ex , hint . syntheticException ) ;
101
- addExceptionTypeValue ( event , 'Custom Object' ) ;
102
- } else {
103
- // If none of previous checks were valid, then it means that
104
- // it's not a DOMError/DOMException
105
- // it's not a plain Object
106
- // it's not a valid ErrorEvent (one with an error property)
107
- // it's not an Error
108
- // So bail out and capture it as a simple message:
109
- const ex = exception as string ;
110
- event = this . eventFromMessage ( ex , undefined , hint ) ;
111
- addExceptionTypeValue ( event , `${ ex } ` ) ;
112
- }
74
+ public eventFromException ( exception : any , hint ?: SentryEventHint ) : SyncPromise < SentryEvent > {
75
+ return new SyncPromise < SentryEvent > ( resolve => {
76
+ let event : SentryEvent ;
77
+
78
+ if ( isErrorEvent ( exception as ErrorEvent ) && ( exception as ErrorEvent ) . error ) {
79
+ // If it is an ErrorEvent with `error` property, extract it to get actual Error
80
+ const ex = exception as ErrorEvent ;
81
+ exception = ex . error ; // tslint:disable-line:no-parameter-reassignment
82
+ event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ;
83
+ resolve ( this . buildEvent ( event ) ) ;
84
+ } else if ( isDOMError ( exception as DOMError ) || isDOMException ( exception as DOMException ) ) {
85
+ // If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
86
+ // then we just extract the name and message, as they don't provide anything else
87
+ // https://developer.mozilla.org/en-US/docs/Web/API/DOMError
88
+ // https://developer.mozilla.org/en-US/docs/Web/API/DOMException
89
+ const ex = exception as DOMException ;
90
+ const name = ex . name || ( isDOMError ( ex ) ? 'DOMError' : 'DOMException' ) ;
91
+ const message = ex . message ? `${ name } : ${ ex . message } ` : name ;
92
+
93
+ this . eventFromMessage ( message , undefined , hint ) . then ( messageEvent => {
94
+ addExceptionTypeValue ( messageEvent , message ) ;
95
+ resolve ( this . buildEvent ( messageEvent ) ) ;
96
+ } ) ;
97
+ } else if ( isError ( exception as Error ) ) {
98
+ // we have a real Error object, do nothing
99
+ event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ;
100
+ resolve ( this . buildEvent ( event ) ) ;
101
+ } else if ( isPlainObject ( exception as { } ) && hint && hint . syntheticException ) {
102
+ // If it is plain Object, serialize it manually and extract options
103
+ // This will allow us to group events based on top-level keys
104
+ // which is much better than creating new group when any key/value change
105
+ const ex = exception as { } ;
106
+ event = eventFromPlainObject ( ex , hint . syntheticException ) ;
107
+ addExceptionTypeValue ( event , 'Custom Object' ) ;
108
+ resolve ( this . buildEvent ( event ) ) ;
109
+ } else {
110
+ // If none of previous checks were valid, then it means that
111
+ // it's not a DOMError/DOMException
112
+ // it's not a plain Object
113
+ // it's not a valid ErrorEvent (one with an error property)
114
+ // it's not an Error
115
+ // So bail out and capture it as a simple message:
116
+ const ex = exception as string ;
117
+ this . eventFromMessage ( ex , undefined , hint ) . then ( messageEvent => {
118
+ addExceptionTypeValue ( messageEvent , `${ ex } ` ) ;
119
+ resolve ( this . buildEvent ( messageEvent ) ) ;
120
+ } ) ;
121
+ }
122
+ } ) ;
123
+ }
113
124
114
- event = {
125
+ /** JSDOC */
126
+ private buildEvent ( event : SentryEvent , hint ?: SentryEventHint ) : SentryEvent {
127
+ return {
115
128
...event ,
116
129
event_id : hint && hint . event_id ,
117
130
exception : {
@@ -122,14 +135,16 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
122
135
} ,
123
136
} ,
124
137
} ;
125
-
126
- return event ;
127
138
}
128
139
129
140
/**
130
141
* @inheritDoc
131
142
*/
132
- public eventFromMessage ( message : string , level : Severity = Severity . Info , hint ?: SentryEventHint ) : SentryEvent {
143
+ public eventFromMessage (
144
+ message : string ,
145
+ level : Severity = Severity . Info ,
146
+ hint ?: SentryEventHint ,
147
+ ) : SyncPromise < SentryEvent > {
133
148
const event : SentryEvent = {
134
149
event_id : hint && hint . event_id ,
135
150
level,
@@ -144,6 +159,6 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
144
159
} ;
145
160
}
146
161
147
- return event ;
162
+ return SyncPromise . resolve ( event ) ;
148
163
}
149
164
}
0 commit comments