@@ -3,13 +3,51 @@ import { fill, getFunctionName, getGlobalObject } from '@sentry/utils';
3
3
4
4
import { wrap } from '../helpers' ;
5
5
6
+ const DEFAULT_EVENT_TARGET = [
7
+ 'EventTarget' ,
8
+ 'Window' ,
9
+ 'Node' ,
10
+ 'ApplicationCache' ,
11
+ 'AudioTrackList' ,
12
+ 'ChannelMergerNode' ,
13
+ 'CryptoOperation' ,
14
+ 'EventSource' ,
15
+ 'FileReader' ,
16
+ 'HTMLUnknownElement' ,
17
+ 'IDBDatabase' ,
18
+ 'IDBRequest' ,
19
+ 'IDBTransaction' ,
20
+ 'KeyOperation' ,
21
+ 'MediaController' ,
22
+ 'MessagePort' ,
23
+ 'ModalWindow' ,
24
+ 'Notification' ,
25
+ 'SVGElementInstance' ,
26
+ 'Screen' ,
27
+ 'TextTrack' ,
28
+ 'TextTrackCue' ,
29
+ 'TextTrackList' ,
30
+ 'WebSocket' ,
31
+ 'WebSocketWorker' ,
32
+ 'Worker' ,
33
+ 'XMLHttpRequest' ,
34
+ 'XMLHttpRequestEventTarget' ,
35
+ 'XMLHttpRequestUpload' ,
36
+ ] ;
37
+
6
38
type XMLHttpRequestProp = 'onload' | 'onerror' | 'onprogress' | 'onreadystatechange' ;
7
39
40
+ /** JSDoc */
41
+ interface TryCatchOptions {
42
+ setTimeout : boolean ;
43
+ setInterval : boolean ;
44
+ requestAnimationFrame : boolean ;
45
+ XMLHttpRequest : boolean ;
46
+ eventTarget : boolean | string [ ] ;
47
+ }
48
+
8
49
/** Wrap timer functions and event targets to catch errors and provide better meta data */
9
50
export class TryCatch implements Integration {
10
- /** JSDoc */
11
- private _ignoreOnError : number = 0 ;
12
-
13
51
/**
14
52
* @inheritDoc
15
53
*/
@@ -20,6 +58,23 @@ export class TryCatch implements Integration {
20
58
*/
21
59
public static id : string = 'TryCatch' ;
22
60
61
+ /** JSDoc */
62
+ private readonly _options : TryCatchOptions ;
63
+
64
+ /**
65
+ * @inheritDoc
66
+ */
67
+ public constructor ( options ?: Partial < TryCatchOptions > ) {
68
+ this . _options = {
69
+ XMLHttpRequest : true ,
70
+ eventTarget : true ,
71
+ requestAnimationFrame : true ,
72
+ setInterval : true ,
73
+ setTimeout : true ,
74
+ ...options ,
75
+ } ;
76
+ }
77
+
23
78
/** JSDoc */
24
79
private _wrapTimeFunction ( original : ( ) => void ) : ( ) => number {
25
80
return function ( this : any , ...args : any [ ] ) : number {
@@ -170,48 +225,27 @@ export class TryCatch implements Integration {
170
225
* and provide better metadata.
171
226
*/
172
227
public setupOnce ( ) : void {
173
- this . _ignoreOnError = this . _ignoreOnError ;
174
-
175
228
const global = getGlobalObject ( ) ;
176
229
177
- fill ( global , 'setTimeout' , this . _wrapTimeFunction . bind ( this ) ) ;
178
- fill ( global , 'setInterval' , this . _wrapTimeFunction . bind ( this ) ) ;
179
- fill ( global , 'requestAnimationFrame' , this . _wrapRAF . bind ( this ) ) ;
230
+ if ( this . _options . setTimeout ) {
231
+ fill ( global , 'setTimeout' , this . _wrapTimeFunction . bind ( this ) ) ;
232
+ }
233
+
234
+ if ( this . _options . setInterval ) {
235
+ fill ( global , 'setInterval' , this . _wrapTimeFunction . bind ( this ) ) ;
236
+ }
180
237
181
- if ( 'XMLHttpRequest' in global ) {
238
+ if ( this . _options . requestAnimationFrame ) {
239
+ fill ( global , 'requestAnimationFrame' , this . _wrapRAF . bind ( this ) ) ;
240
+ }
241
+
242
+ if ( this . _options . XMLHttpRequest && 'XMLHttpRequest' in global ) {
182
243
fill ( XMLHttpRequest . prototype , 'send' , this . _wrapXHR . bind ( this ) ) ;
183
244
}
184
245
185
- [
186
- 'EventTarget' ,
187
- 'Window' ,
188
- 'Node' ,
189
- 'ApplicationCache' ,
190
- 'AudioTrackList' ,
191
- 'ChannelMergerNode' ,
192
- 'CryptoOperation' ,
193
- 'EventSource' ,
194
- 'FileReader' ,
195
- 'HTMLUnknownElement' ,
196
- 'IDBDatabase' ,
197
- 'IDBRequest' ,
198
- 'IDBTransaction' ,
199
- 'KeyOperation' ,
200
- 'MediaController' ,
201
- 'MessagePort' ,
202
- 'ModalWindow' ,
203
- 'Notification' ,
204
- 'SVGElementInstance' ,
205
- 'Screen' ,
206
- 'TextTrack' ,
207
- 'TextTrackCue' ,
208
- 'TextTrackList' ,
209
- 'WebSocket' ,
210
- 'WebSocketWorker' ,
211
- 'Worker' ,
212
- 'XMLHttpRequest' ,
213
- 'XMLHttpRequestEventTarget' ,
214
- 'XMLHttpRequestUpload' ,
215
- ] . forEach ( this . _wrapEventTarget . bind ( this ) ) ;
246
+ if ( this . _options . eventTarget ) {
247
+ const eventTarget = Array . isArray ( this . _options . eventTarget ) ? this . _options . eventTarget : DEFAULT_EVENT_TARGET ;
248
+ eventTarget . forEach ( this . _wrapEventTarget . bind ( this ) ) ;
249
+ }
216
250
}
217
251
}
0 commit comments