@@ -2,7 +2,7 @@ import { getCurrentHub } from '@sentry/core';
2
2
import { Event , Integration , Severity } from '@sentry/types' ;
3
3
import {
4
4
addExceptionMechanism ,
5
- getGlobalObject ,
5
+ addInstrumentationHandler ,
6
6
getLocationHref ,
7
7
isErrorEvent ,
8
8
isPrimitive ,
@@ -34,15 +34,6 @@ export class GlobalHandlers implements Integration {
34
34
/** JSDoc */
35
35
private readonly _options : GlobalHandlersIntegrations ;
36
36
37
- /** JSDoc */
38
- private readonly _global : Window = getGlobalObject ( ) ;
39
-
40
- /** JSDoc */
41
- private _oldOnErrorHandler : OnErrorEventHandler = null ;
42
-
43
- /** JSDoc */
44
- private _oldOnUnhandledRejectionHandler : ( ( e : any ) => void ) | null = null ;
45
-
46
37
/** JSDoc */
47
38
private _onErrorHandlerInstalled : boolean = false ;
48
39
@@ -80,49 +71,41 @@ export class GlobalHandlers implements Integration {
80
71
return ;
81
72
}
82
73
83
- const self = this ; // tslint:disable-line:no-this-assignment
84
- this . _oldOnErrorHandler = this . _global . onerror ;
74
+ addInstrumentationHandler ( {
75
+ callback : ( data : { msg : any ; url : any ; line : any ; column : any ; error : any } ) => {
76
+ const error = data . error ;
77
+ const currentHub = getCurrentHub ( ) ;
78
+ const hasIntegration = currentHub . getIntegration ( GlobalHandlers ) ;
79
+ const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
85
80
86
- this . _global . onerror = function ( msg : any , url : any , line : any , column : any , error : any ) : boolean {
87
- const currentHub = getCurrentHub ( ) ;
88
- const hasIntegration = currentHub . getIntegration ( GlobalHandlers ) ;
89
- const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
90
-
91
- if ( ! hasIntegration || shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
92
- if ( self . _oldOnErrorHandler ) {
93
- return self . _oldOnErrorHandler . apply ( this , arguments ) ;
81
+ if ( ! hasIntegration || shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
82
+ return ;
94
83
}
95
- return false ;
96
- }
97
-
98
- const client = currentHub . getClient ( ) ;
99
- const event = isPrimitive ( error )
100
- ? self . _eventFromIncompleteOnError ( msg , url , line , column )
101
- : self . _enhanceEventWithInitialFrame (
102
- eventFromUnknownInput ( error , undefined , {
103
- attachStacktrace : client && client . getOptions ( ) . attachStacktrace ,
104
- rejection : false ,
105
- } ) ,
106
- url ,
107
- line ,
108
- column ,
109
- ) ;
110
-
111
- addExceptionMechanism ( event , {
112
- handled : false ,
113
- type : 'onerror' ,
114
- } ) ;
115
-
116
- currentHub . captureEvent ( event , {
117
- originalException : error ,
118
- } ) ;
119
-
120
- if ( self . _oldOnErrorHandler ) {
121
- return self . _oldOnErrorHandler . apply ( this , arguments ) ;
122
- }
123
84
124
- return false ;
125
- } ;
85
+ const client = currentHub . getClient ( ) ;
86
+ const event = isPrimitive ( error )
87
+ ? this . _eventFromIncompleteOnError ( data . msg , data . url , data . line , data . column )
88
+ : this . _enhanceEventWithInitialFrame (
89
+ eventFromUnknownInput ( error , undefined , {
90
+ attachStacktrace : client && client . getOptions ( ) . attachStacktrace ,
91
+ rejection : false ,
92
+ } ) ,
93
+ data . url ,
94
+ data . line ,
95
+ data . column ,
96
+ ) ;
97
+
98
+ addExceptionMechanism ( event , {
99
+ handled : false ,
100
+ type : 'onerror' ,
101
+ } ) ;
102
+
103
+ currentHub . captureEvent ( event , {
104
+ originalException : error ,
105
+ } ) ;
106
+ } ,
107
+ type : 'error' ,
108
+ } ) ;
126
109
127
110
this . _onErrorHandlerInstalled = true ;
128
111
}
@@ -133,67 +116,60 @@ export class GlobalHandlers implements Integration {
133
116
return ;
134
117
}
135
118
136
- const self = this ; // tslint:disable-line:no-this-assignment
137
- this . _oldOnUnhandledRejectionHandler = this . _global . onunhandledrejection ;
138
-
139
- this . _global . onunhandledrejection = function ( e : any ) : boolean {
140
- let error = e ;
141
-
142
- // dig the object of the rejection out of known event types
143
- try {
144
- // PromiseRejectionEvents store the object of the rejection under 'reason'
145
- // see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
146
- if ( 'reason' in e ) {
147
- error = e . reason ;
119
+ addInstrumentationHandler ( {
120
+ callback : ( e : any ) => {
121
+ let error = e ;
122
+
123
+ // dig the object of the rejection out of known event types
124
+ try {
125
+ // PromiseRejectionEvents store the object of the rejection under 'reason'
126
+ // see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
127
+ if ( 'reason' in e ) {
128
+ error = e . reason ;
129
+ }
130
+ // something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
131
+ // to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
132
+ // the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
133
+ // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
134
+ // https://github.com/getsentry/sentry-javascript/issues/2380
135
+ else if ( 'detail' in e && 'reason' in e . detail ) {
136
+ error = e . detail . reason ;
137
+ }
138
+ } catch ( _oO ) {
139
+ // no-empty
148
140
}
149
- // something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
150
- // to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
151
- // the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
152
- // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
153
- // https://github.com/getsentry/sentry-javascript/issues/2380
154
- else if ( 'detail' in e && 'reason' in e . detail ) {
155
- error = e . detail . reason ;
156
- }
157
- } catch ( _oO ) {
158
- // no-empty
159
- }
160
141
161
- const currentHub = getCurrentHub ( ) ;
162
- const hasIntegration = currentHub . getIntegration ( GlobalHandlers ) ;
163
- const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
142
+ const currentHub = getCurrentHub ( ) ;
143
+ const hasIntegration = currentHub . getIntegration ( GlobalHandlers ) ;
144
+ const isFailedOwnDelivery = error && error . __sentry_own_request__ === true ;
164
145
165
- if ( ! hasIntegration || shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
166
- if ( self . _oldOnUnhandledRejectionHandler ) {
167
- return self . _oldOnUnhandledRejectionHandler . apply ( this , arguments ) ;
146
+ if ( ! hasIntegration || shouldIgnoreOnError ( ) || isFailedOwnDelivery ) {
147
+ return true ;
168
148
}
169
- return true ;
170
- }
171
149
172
- const client = currentHub . getClient ( ) ;
173
- const event = isPrimitive ( error )
174
- ? self . _eventFromIncompleteRejection ( error )
175
- : eventFromUnknownInput ( error , undefined , {
176
- attachStacktrace : client && client . getOptions ( ) . attachStacktrace ,
177
- rejection : true ,
178
- } ) ;
150
+ const client = currentHub . getClient ( ) ;
151
+ const event = isPrimitive ( error )
152
+ ? this . _eventFromIncompleteRejection ( error )
153
+ : eventFromUnknownInput ( error , undefined , {
154
+ attachStacktrace : client && client . getOptions ( ) . attachStacktrace ,
155
+ rejection : true ,
156
+ } ) ;
179
157
180
- event . level = Severity . Error ;
158
+ event . level = Severity . Error ;
181
159
182
- addExceptionMechanism ( event , {
183
- handled : false ,
184
- type : 'onunhandledrejection' ,
185
- } ) ;
160
+ addExceptionMechanism ( event , {
161
+ handled : false ,
162
+ type : 'onunhandledrejection' ,
163
+ } ) ;
186
164
187
- currentHub . captureEvent ( event , {
188
- originalException : error ,
189
- } ) ;
165
+ currentHub . captureEvent ( event , {
166
+ originalException : error ,
167
+ } ) ;
190
168
191
- if ( self . _oldOnUnhandledRejectionHandler ) {
192
- return self . _oldOnUnhandledRejectionHandler . apply ( this , arguments ) ;
193
- }
194
-
195
- return true ;
196
- } ;
169
+ return ;
170
+ } ,
171
+ type : 'unhandledrejection' ,
172
+ } ) ;
197
173
198
174
this . _onUnhandledRejectionHandlerInstalled = true ;
199
175
}
0 commit comments