@@ -13,11 +13,12 @@ import {
13
13
import { eventFromUnknownInput } from '../eventbuilder' ;
14
14
import { shouldIgnoreOnError } from '../helpers' ;
15
15
16
+ type GlobalHandlersIntegrationsOptionKeys = 'onerror' | 'onunhandledrejection' ;
17
+
16
18
/** JSDoc */
17
- interface GlobalHandlersIntegrations {
18
- onerror : boolean ;
19
- onunhandledrejection : boolean ;
20
- }
19
+ type GlobalHandlersIntegrations = Record < GlobalHandlersIntegrationsOptionKeys , boolean > ;
20
+
21
+ type InstallFunc = ( hub : Hub , attachStacktrace : boolean | undefined ) => void ;
21
22
22
23
/** Global handlers */
23
24
export class GlobalHandlers implements Integration {
@@ -34,11 +35,14 @@ export class GlobalHandlers implements Integration {
34
35
/** JSDoc */
35
36
private readonly _options : GlobalHandlersIntegrations ;
36
37
37
- /** JSDoc */
38
- private _onErrorHandlerInstalled : boolean = false ;
39
-
40
- /** JSDoc */
41
- private _onUnhandledRejectionHandlerInstalled : boolean = false ;
38
+ /**
39
+ * Stores references functions to installing handlers. Will set to undefined
40
+ * after they have been run so that they are not used twice.
41
+ */
42
+ private _installFunc : Record < GlobalHandlersIntegrationsOptionKeys , InstallFunc | undefined > = {
43
+ onerror : _installGlobalOnErrorHandler ,
44
+ onunhandledrejection : _installGlobalOnUnhandledRejectionHandler ,
45
+ } ;
42
46
43
47
/** JSDoc */
44
48
public constructor ( options ?: GlobalHandlersIntegrations ) {
@@ -57,17 +61,18 @@ export class GlobalHandlers implements Integration {
57
61
const hub = getCurrentHub ( ) ;
58
62
const client = hub . getClient ( ) ;
59
63
const attachStacktrace = client && client . getOptions ( ) . attachStacktrace ;
60
-
61
- if ( this . _options . onerror || ! this . _onErrorHandlerInstalled ) {
62
- globalHandlerLog ( 'onerror' ) ;
63
- _installGlobalOnErrorHandler ( hub , attachStacktrace ) ;
64
- this . _onErrorHandlerInstalled = true ;
65
- }
66
-
67
- if ( this . _options . onunhandledrejection || ! this . _onUnhandledRejectionHandlerInstalled ) {
68
- globalHandlerLog ( 'onunhandledrejection' ) ;
69
- _installGlobalOnUnhandledRejectionHandler ( hub , attachStacktrace ) ;
70
- this . _onUnhandledRejectionHandlerInstalled = true ;
64
+ const options = this . _options ;
65
+
66
+ // We can disable guard-for-in as we construct the options object above + do checks against
67
+ // `this._installFunc` for the property.
68
+ // eslint-disable-next-line guard-for-in
69
+ for ( const key in options ) {
70
+ const installFunc = this . _installFunc [ key as GlobalHandlersIntegrationsOptionKeys ] ;
71
+ if ( installFunc && options [ key as GlobalHandlersIntegrationsOptionKeys ] ) {
72
+ globalHandlerLog ( key ) ;
73
+ installFunc ( hub , attachStacktrace ) ;
74
+ this . _installFunc [ key as GlobalHandlersIntegrationsOptionKeys ] = undefined ;
75
+ }
71
76
}
72
77
}
73
78
}
@@ -81,9 +86,7 @@ function _installGlobalOnErrorHandler(hub: Hub, attachStacktrace: boolean | unde
81
86
return ;
82
87
}
83
88
const { msg, url, line, column, error } = data ;
84
- const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
85
-
86
- if ( shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
89
+ if ( shouldIgnoreOnError ( ) || ( error && error . __sentry_own_request__ ) ) {
87
90
return ;
88
91
}
89
92
@@ -135,8 +138,7 @@ function _installGlobalOnUnhandledRejectionHandler(hub: Hub, attachStacktrace: b
135
138
// no-empty
136
139
}
137
140
138
- const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
139
- if ( shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
141
+ if ( shouldIgnoreOnError ( ) || ( error && error . __sentry_own_request__ ) ) {
140
142
return true ;
141
143
}
142
144
0 commit comments