Skip to content

Commit 2efbc98

Browse files
authored
feat: TryCatch Integration options (getsentry#2601)
* feat: TryCatch Integration options * Use <Partial> for options types
1 parent 4910510 commit 2efbc98

File tree

3 files changed

+84
-49
lines changed

3 files changed

+84
-49
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- [core] feat: Send transactions in envelopes (#2553)
1010
- [core] fix: Send event timestamp (#2575)
11+
- [browser] feat: Allow for configuring TryCatch integration (#2601)
1112
- [browser] fix: Call wrapped `RequestAnimationFrame` with correct context (#2570)
1213
- [node] fix: Prevent reading the same source file multiple times (#2569)
1314
- [integrations] feat: Vue performance monitoring (#2571)

packages/browser/src/integrations/breadcrumbs.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
2525
}
2626

2727
/** JSDoc */
28-
interface BreadcrumbIntegrations {
29-
console?: boolean;
30-
dom?: boolean;
31-
fetch?: boolean;
32-
history?: boolean;
33-
sentry?: boolean;
34-
xhr?: boolean;
28+
interface BreadcrumbsOptions {
29+
console: boolean;
30+
dom: boolean;
31+
fetch: boolean;
32+
history: boolean;
33+
sentry: boolean;
34+
xhr: boolean;
3535
}
3636

3737
/**
@@ -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?: Partial<BreadcrumbsOptions>) {
5959
this._options = {
6060
console: true,
6161
dom: true,

packages/browser/src/integrations/trycatch.ts

+74-40
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,51 @@ 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+
/** 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 */
950
export 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

Comments
 (0)