@@ -3,13 +3,51 @@ import { fill, getFunctionName, getGlobalObject } from '@sentry/utils';
33
44import { wrap } from '../helpers' ;
55
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+
638type XMLHttpRequestProp = 'onload' | 'onerror' | 'onprogress' | 'onreadystatechange' ;
739
40+ /** JSDoc */
41+ interface TryCatchOptions {
42+ setTimeout : boolean ;
43+ setInterval : boolean ;
44+ requestAnimationFrame : boolean ;
45+ XMLHttpRequest : boolean ;
46+ eventTarget : boolean | string [ ] ;
47+ }
48+
849/** Wrap timer functions and event targets to catch errors and provide better meta data */
950export class TryCatch implements Integration {
10- /** JSDoc */
11- private _ignoreOnError : number = 0 ;
12-
1351 /**
1452 * @inheritDoc
1553 */
@@ -20,6 +58,23 @@ export class TryCatch implements Integration {
2058 */
2159 public static id : string = 'TryCatch' ;
2260
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+
2378 /** JSDoc */
2479 private _wrapTimeFunction ( original : ( ) => void ) : ( ) => number {
2580 return function ( this : any , ...args : any [ ] ) : number {
@@ -170,48 +225,27 @@ export class TryCatch implements Integration {
170225 * and provide better metadata.
171226 */
172227 public setupOnce ( ) : void {
173- this . _ignoreOnError = this . _ignoreOnError ;
174-
175228 const global = getGlobalObject ( ) ;
176229
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+ }
180237
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 ) {
182243 fill ( XMLHttpRequest . prototype , 'send' , this . _wrapXHR . bind ( this ) ) ;
183244 }
184245
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+ }
216250 }
217251}
0 commit comments