11import 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' ;
34import { 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' ;
56import { eventFromUnknownInput , isPrimitive } from '@sentry/utils' ;
67
78type GlobalHandlersIntegrationsOptionKeys = 'error' | 'unhandledrejection' ;
@@ -26,15 +27,6 @@ export class GlobalHandlers implements Integration {
2627 /** JSDoc */
2728 private readonly _options : GlobalHandlersIntegrations ;
2829
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-
3830 /** JSDoc */
3931 public constructor ( options ?: GlobalHandlersIntegrations ) {
4032 this . _options = {
@@ -47,35 +39,35 @@ export class GlobalHandlers implements Integration {
4739 * @inheritDoc
4840 */
4941 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 ) ;
6152 }
6253 }
6354}
6455
65- function installGlobalErrorHandler ( ) : void {
56+ function installGlobalErrorHandler ( client : Client ) : void {
6657 globalThis . addEventListener ( 'error' , data => {
67- if ( isExiting ) {
58+ if ( getClient ( ) !== client || isExiting ) {
6859 return ;
6960 }
7061
71- const [ hub , stackParser ] = getHubAndOptions ( ) ;
62+ const stackParser = getStackParser ( ) ;
63+
7264 const { message, error } = data ;
7365
7466 const event = eventFromUnknownInput ( getClient ( ) , stackParser , error || message ) ;
7567
7668 event . level = 'fatal' ;
7769
78- hub . captureEvent ( event , {
70+ captureEvent ( event , {
7971 originalException : error ,
8072 mechanism : {
8173 handled : false ,
@@ -94,13 +86,13 @@ function installGlobalErrorHandler(): void {
9486 } ) ;
9587}
9688
97- function installGlobalUnhandledRejectionHandler ( ) : void {
89+ function installGlobalUnhandledRejectionHandler ( client : Client ) : void {
9890 globalThis . addEventListener ( 'unhandledrejection' , ( e : PromiseRejectionEvent ) => {
99- if ( isExiting ) {
91+ if ( getClient ( ) !== client || isExiting ) {
10092 return ;
10193 }
10294
103- const [ hub , stackParser ] = getHubAndOptions ( ) ;
95+ const stackParser = getStackParser ( ) ;
10496 let error = e ;
10597
10698 // dig the object of the rejection out of known event types
@@ -118,7 +110,7 @@ function installGlobalUnhandledRejectionHandler(): void {
118110
119111 event . level = 'fatal' ;
120112
121- hub . captureEvent ( event , {
113+ captureEvent ( event , {
122114 originalException : error ,
123115 mechanism : {
124116 handled : false ,
@@ -157,12 +149,12 @@ function eventFromRejectionWithPrimitive(reason: Primitive): Event {
157149 } ;
158150}
159151
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 ;
168160}
0 commit comments