1
1
import type { ServerRuntimeClient } from '@sentry/core' ;
2
- import { getClient , getCurrentHub , getCurrentScope } from '@sentry/core' ;
2
+ import { captureEvent } from '@sentry/core' ;
3
+ import { getClient } from '@sentry/core' ;
3
4
import { flush } from '@sentry/core' ;
4
- import type { Event , Hub , Integration , Primitive , StackParser } from '@sentry/types' ;
5
+ import type { Client , Event , Integration , Primitive , StackParser } from '@sentry/types' ;
5
6
import { eventFromUnknownInput , isPrimitive } from '@sentry/utils' ;
6
7
7
8
type GlobalHandlersIntegrationsOptionKeys = 'error' | 'unhandledrejection' ;
@@ -26,15 +27,6 @@ export class GlobalHandlers implements Integration {
26
27
/** JSDoc */
27
28
private readonly _options : GlobalHandlersIntegrations ;
28
29
29
- /**
30
- * Stores references functions to installing handlers. Will set to undefined
31
- * after they have been run so that they are not used twice.
32
- */
33
- private _installFunc : Record < GlobalHandlersIntegrationsOptionKeys , ( ( ) => void ) | undefined > = {
34
- error : installGlobalErrorHandler ,
35
- unhandledrejection : installGlobalUnhandledRejectionHandler ,
36
- } ;
37
-
38
30
/** JSDoc */
39
31
public constructor ( options ?: GlobalHandlersIntegrations ) {
40
32
this . _options = {
@@ -47,35 +39,35 @@ export class GlobalHandlers implements Integration {
47
39
* @inheritDoc
48
40
*/
49
41
public setupOnce ( ) : void {
50
- const options = this . _options ;
51
-
52
- // We can disable guard-for-in as we construct the options object above + do checks against
53
- // `this._installFunc` for the property.
54
- // eslint-disable-next-line guard-for-in
55
- for ( const key in options ) {
56
- const installFunc = this . _installFunc [ key as GlobalHandlersIntegrationsOptionKeys ] ;
57
- if ( installFunc && options [ key as GlobalHandlersIntegrationsOptionKeys ] ) {
58
- installFunc ( ) ;
59
- this . _installFunc [ key as GlobalHandlersIntegrationsOptionKeys ] = undefined ;
60
- }
42
+ // noop
43
+ }
44
+
45
+ /** @inheritdoc */
46
+ public setup ( client : Client ) : void {
47
+ if ( this . _options . error ) {
48
+ installGlobalErrorHandler ( client ) ;
49
+ }
50
+ if ( this . _options . unhandledrejection ) {
51
+ installGlobalUnhandledRejectionHandler ( client ) ;
61
52
}
62
53
}
63
54
}
64
55
65
- function installGlobalErrorHandler ( ) : void {
56
+ function installGlobalErrorHandler ( client : Client ) : void {
66
57
globalThis . addEventListener ( 'error' , data => {
67
- if ( isExiting ) {
58
+ if ( getClient ( ) !== client || isExiting ) {
68
59
return ;
69
60
}
70
61
71
- const [ hub , stackParser ] = getHubAndOptions ( ) ;
62
+ const stackParser = getStackParser ( ) ;
63
+
72
64
const { message, error } = data ;
73
65
74
66
const event = eventFromUnknownInput ( getClient ( ) , stackParser , error || message ) ;
75
67
76
68
event . level = 'fatal' ;
77
69
78
- hub . captureEvent ( event , {
70
+ captureEvent ( event , {
79
71
originalException : error ,
80
72
mechanism : {
81
73
handled : false ,
@@ -94,13 +86,13 @@ function installGlobalErrorHandler(): void {
94
86
} ) ;
95
87
}
96
88
97
- function installGlobalUnhandledRejectionHandler ( ) : void {
89
+ function installGlobalUnhandledRejectionHandler ( client : Client ) : void {
98
90
globalThis . addEventListener ( 'unhandledrejection' , ( e : PromiseRejectionEvent ) => {
99
- if ( isExiting ) {
91
+ if ( getClient ( ) !== client || isExiting ) {
100
92
return ;
101
93
}
102
94
103
- const [ hub , stackParser ] = getHubAndOptions ( ) ;
95
+ const stackParser = getStackParser ( ) ;
104
96
let error = e ;
105
97
106
98
// dig the object of the rejection out of known event types
@@ -118,7 +110,7 @@ function installGlobalUnhandledRejectionHandler(): void {
118
110
119
111
event . level = 'fatal' ;
120
112
121
- hub . captureEvent ( event , {
113
+ captureEvent ( event , {
122
114
originalException : error ,
123
115
mechanism : {
124
116
handled : false ,
@@ -157,12 +149,12 @@ function eventFromRejectionWithPrimitive(reason: Primitive): Event {
157
149
} ;
158
150
}
159
151
160
- function getHubAndOptions ( ) : [ Hub , StackParser ] {
161
- const hub = getCurrentHub ( ) ;
162
- const client = hub . getClient < ServerRuntimeClient > ( ) ;
163
- const options = ( client && client . getOptions ( ) ) || {
164
- stackParser : ( ) => [ ] ,
165
- attachStacktrace : false ,
166
- } ;
167
- return [ hub , options . stackParser ] ;
152
+ function getStackParser ( ) : StackParser {
153
+ const client = getClient < ServerRuntimeClient > ( ) ;
154
+
155
+ if ( ! client ) {
156
+ return ( ) => [ ] ;
157
+ }
158
+
159
+ return client . getOptions ( ) . stackParser ;
168
160
}
0 commit comments