11/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22import { 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' ;
124import {
135 addExceptionMechanism ,
146 addGlobalErrorInstrumentationHandler ,
@@ -85,7 +77,6 @@ export class GlobalHandlers implements Integration {
8577 }
8678}
8779
88- /** JSDoc */
8980function _installGlobalOnErrorHandler ( ) : void {
9081 addGlobalErrorInstrumentationHandler ( data => {
9182 const [ hub , stackParser , attachStacktrace ] = getHubAndOptions ( ) ;
@@ -113,38 +104,19 @@ function _installGlobalOnErrorHandler(): void {
113104 } ) ;
114105}
115106
116- /** JSDoc */
117107function _installGlobalOnUnhandledRejectionHandler ( ) : void {
118108 addGlobalUnhandledRejectionInstrumentationHandler ( e => {
119109 const [ hub , stackParser , attachStacktrace ] = getHubAndOptions ( ) ;
120110 if ( ! hub . getIntegration ( GlobalHandlers ) ) {
121111 return ;
122112 }
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- }
143113
144114 if ( shouldIgnoreOnError ( ) ) {
145115 return true ;
146116 }
147117
118+ const error = _getUnhandledRejectionError ( e as unknown ) ;
119+
148120 const event = isPrimitive ( error )
149121 ? _eventFromRejectionWithPrimitive ( error )
150122 : eventFromUnknownInput ( stackParser , error , undefined , attachStacktrace , true ) ;
@@ -156,6 +128,35 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
156128 } ) ;
157129}
158130
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+
159160/**
160161 * Create an event from a promise rejection where the `reason` is a primitive.
161162 *
0 commit comments