1
1
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
2
import { getCurrentHub } from '@sentry/core' ;
3
- import type {
4
- Event ,
5
- EventHint ,
6
- HandlerDataUnhandledRejection ,
7
- Hub ,
8
- Integration ,
9
- Primitive ,
10
- StackParser ,
11
- } from '@sentry/types' ;
3
+ import type { Event , EventHint , Hub , Integration , Primitive , StackParser } from '@sentry/types' ;
12
4
import {
13
5
addExceptionMechanism ,
14
6
addGlobalErrorInstrumentationHandler ,
@@ -85,7 +77,6 @@ export class GlobalHandlers implements Integration {
85
77
}
86
78
}
87
79
88
- /** JSDoc */
89
80
function _installGlobalOnErrorHandler ( ) : void {
90
81
addGlobalErrorInstrumentationHandler ( data => {
91
82
const [ hub , stackParser , attachStacktrace ] = getHubAndOptions ( ) ;
@@ -113,38 +104,19 @@ function _installGlobalOnErrorHandler(): void {
113
104
} ) ;
114
105
}
115
106
116
- /** JSDoc */
117
107
function _installGlobalOnUnhandledRejectionHandler ( ) : void {
118
108
addGlobalUnhandledRejectionInstrumentationHandler ( e => {
119
109
const [ hub , stackParser , attachStacktrace ] = getHubAndOptions ( ) ;
120
110
if ( ! hub . getIntegration ( GlobalHandlers ) ) {
121
111
return ;
122
112
}
123
- let error : Error | HandlerDataUnhandledRejection = e ;
124
-
125
- // dig the object of the rejection out of known event types
126
- try {
127
- // PromiseRejectionEvents store the object of the rejection under 'reason'
128
- // see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
129
- if ( 'reason' in e && e . reason ) {
130
- error = e . reason ;
131
- }
132
- // something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
133
- // to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
134
- // the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
135
- // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
136
- // https://github.com/getsentry/sentry-javascript/issues/2380
137
- else if ( 'detail' in e && e . detail && 'reason' in e . detail && e . detail . reason ) {
138
- error = e . detail . reason ;
139
- }
140
- } catch ( _oO ) {
141
- // no-empty
142
- }
143
113
144
114
if ( shouldIgnoreOnError ( ) ) {
145
115
return true ;
146
116
}
147
117
118
+ const error = _getUnhandledRejectionError ( e as unknown ) ;
119
+
148
120
const event = isPrimitive ( error )
149
121
? _eventFromRejectionWithPrimitive ( error )
150
122
: eventFromUnknownInput ( stackParser , error , undefined , attachStacktrace , true ) ;
@@ -156,6 +128,35 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
156
128
} ) ;
157
129
}
158
130
131
+ function _getUnhandledRejectionError ( error : unknown ) : unknown {
132
+ if ( isPrimitive ( error ) ) {
133
+ return error ;
134
+ }
135
+
136
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
137
+ const e = error as any ;
138
+
139
+ // dig the object of the rejection out of known event types
140
+ try {
141
+ // PromiseRejectionEvents store the object of the rejection under 'reason'
142
+ // see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
143
+ if ( 'reason' in e ) {
144
+ return e . reason ;
145
+ }
146
+
147
+ // something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
148
+ // to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
149
+ // the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
150
+ // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
151
+ // https://github.com/getsentry/sentry-javascript/issues/2380
152
+ else if ( 'detail' in e && 'reason' in e . detail ) {
153
+ return e . detail . reason ;
154
+ }
155
+ } catch { } // eslint-disable-line no-empty
156
+
157
+ return error ;
158
+ }
159
+
159
160
/**
160
161
* Create an event from a promise rejection where the `reason` is a primitive.
161
162
*
0 commit comments