Skip to content

Commit 09718ba

Browse files
committed
feat: TryCatch Integration options
1 parent 4910510 commit 09718ba

File tree

2 files changed

+74
-43
lines changed

2 files changed

+74
-43
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
2525
}
2626

2727
/** JSDoc */
28-
interface BreadcrumbIntegrations {
28+
interface BreadcrumbsOptions {
2929
console?: boolean;
3030
dom?: boolean;
3131
fetch?: boolean;
@@ -50,12 +50,12 @@ export class Breadcrumbs implements Integration {
5050
public static id: string = 'Breadcrumbs';
5151

5252
/** JSDoc */
53-
private readonly _options: BreadcrumbIntegrations;
53+
private readonly _options: BreadcrumbsOptions;
5454

5555
/**
5656
* @inheritDoc
5757
*/
58-
public constructor(options?: BreadcrumbIntegrations) {
58+
public constructor(options?: BreadcrumbsOptions) {
5959
this._options = {
6060
console: true,
6161
dom: true,

packages/browser/src/integrations/trycatch.ts

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,50 @@ import { fill, getFunctionName, getGlobalObject } from '@sentry/utils';
33

44
import { 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+
638
type XMLHttpRequestProp = 'onload' | 'onerror' | 'onprogress' | 'onreadystatechange';
739

40+
interface TryCatchOptions {
41+
setTimeout?: boolean;
42+
setInterval?: boolean;
43+
requestAnimationFrame?: boolean;
44+
XMLHttpRequest?: boolean;
45+
eventTarget: boolean | string[];
46+
}
47+
848
/** Wrap timer functions and event targets to catch errors and provide better meta data */
949
export class TryCatch implements Integration {
10-
/** JSDoc */
11-
private _ignoreOnError: number = 0;
12-
1350
/**
1451
* @inheritDoc
1552
*/
@@ -20,6 +57,23 @@ export class TryCatch implements Integration {
2057
*/
2158
public static id: string = 'TryCatch';
2259

60+
/** JSDoc */
61+
private readonly _options: TryCatchOptions;
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public constructor(options?: TryCatchOptions) {
67+
this._options = {
68+
setTimeout: true,
69+
setInterval: true,
70+
requestAnimationFrame: true,
71+
XMLHttpRequest: true,
72+
eventTarget: true,
73+
...options,
74+
};
75+
}
76+
2377
/** JSDoc */
2478
private _wrapTimeFunction(original: () => void): () => number {
2579
return function(this: any, ...args: any[]): number {
@@ -170,48 +224,25 @@ export class TryCatch implements Integration {
170224
* and provide better metadata.
171225
*/
172226
public setupOnce(): void {
173-
this._ignoreOnError = this._ignoreOnError;
174-
175227
const global = getGlobalObject();
176228

177-
fill(global, 'setTimeout', this._wrapTimeFunction.bind(this));
178-
fill(global, 'setInterval', this._wrapTimeFunction.bind(this));
179-
fill(global, 'requestAnimationFrame', this._wrapRAF.bind(this));
229+
if (this._options.setTimeout) {
230+
fill(global, 'setTimeout', this._wrapTimeFunction.bind(this));
231+
}
232+
if (this._options.setInterval) {
233+
fill(global, 'setInterval', this._wrapTimeFunction.bind(this));
234+
}
235+
if (this._options.requestAnimationFrame) {
236+
fill(global, 'requestAnimationFrame', this._wrapRAF.bind(this));
237+
}
180238

181-
if ('XMLHttpRequest' in global) {
239+
if (this._options.XMLHttpRequest && 'XMLHttpRequest' in global) {
182240
fill(XMLHttpRequest.prototype, 'send', this._wrapXHR.bind(this));
183241
}
184242

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));
243+
if (this._options.eventTarget) {
244+
const eventTarget = Array.isArray(this._options.eventTarget) ? this._options.eventTarget : DEFAULT_EVENT_TARGET;
245+
eventTarget.forEach(this._wrapEventTarget.bind(this));
246+
}
216247
}
217248
}

0 commit comments

Comments
 (0)