/
helpers.ts
225 lines (195 loc) · 6.18 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { API, captureException, withScope } from '@sentry/core';
import { DsnLike, Event as SentryEvent, Mechanism, Scope, WrappedFunction } from '@sentry/types';
import { addExceptionMechanism, addExceptionTypeValue, getGlobalObject, logger } from '@sentry/utils';
const global = getGlobalObject<Window>();
let ignoreOnError: number = 0;
/**
* @hidden
*/
export function shouldIgnoreOnError(): boolean {
return ignoreOnError > 0;
}
/**
* @hidden
*/
export function ignoreNextOnError(): void {
// onerror should trigger before setTimeout
ignoreOnError += 1;
setTimeout(() => {
ignoreOnError -= 1;
});
}
/**
* Instruments the given function and sends an event to Sentry every time the
* function throws an exception.
*
* @param fn A function to wrap.
* @returns The wrapped function.
* @hidden
*/
export function wrap(
fn: WrappedFunction,
options: {
mechanism?: Mechanism;
} = {},
before?: WrappedFunction,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any {
if (typeof fn !== 'function') {
return fn;
}
try {
// We don't wanna wrap it twice
if (fn.__sentry__) {
return fn;
}
// If this has already been wrapped in the past, return that wrapped function
if (fn.__sentry_wrapped__) {
return fn.__sentry_wrapped__;
}
} catch (e) {
// Just accessing custom props in some Selenium environments
// can cause a "Permission denied" exception (see raven-js#495).
// Bail on wrapping and return the function as-is (defers to window.onerror).
return fn;
}
/* eslint-disable prefer-rest-params */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sentryWrapped: WrappedFunction = function(this: any): void {
const args = Array.prototype.slice.call(arguments);
try {
if (before && typeof before === 'function') {
before.apply(this, arguments);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const wrappedArguments = args.map((arg: any) => wrap(arg, options));
if (fn.handleEvent) {
// Attempt to invoke user-land function
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it
// means the sentry.javascript SDK caught an error invoking your application code. This
// is expected behavior and NOT indicative of a bug with sentry.javascript.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return fn.handleEvent.apply(this, wrappedArguments);
}
// Attempt to invoke user-land function
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it
// means the sentry.javascript SDK caught an error invoking your application code. This
// is expected behavior and NOT indicative of a bug with sentry.javascript.
return fn.apply(this, wrappedArguments);
} catch (ex) {
ignoreNextOnError();
withScope((scope: Scope) => {
scope.addEventProcessor((event: SentryEvent) => {
const processedEvent = { ...event };
if (options.mechanism) {
addExceptionTypeValue(processedEvent, undefined, undefined);
addExceptionMechanism(processedEvent, options.mechanism);
}
processedEvent.extra = {
...processedEvent.extra,
arguments: args,
};
return processedEvent;
});
captureException(ex);
});
throw ex;
}
};
/* eslint-enable prefer-rest-params */
// Accessing some objects may throw
// ref: https://github.com/getsentry/sentry-javascript/issues/1168
try {
for (const property in fn) {
if (Object.prototype.hasOwnProperty.call(fn, property)) {
sentryWrapped[property] = fn[property];
}
}
} catch (_oO) {} // eslint-disable-line no-empty
fn.prototype = fn.prototype || {};
sentryWrapped.prototype = fn.prototype;
Object.defineProperty(fn, '__sentry_wrapped__', {
enumerable: false,
value: sentryWrapped,
});
// Signal that this function has been wrapped/filled already
// for both debugging and to prevent it to being wrapped/filled twice
Object.defineProperties(sentryWrapped, {
__sentry__: {
enumerable: false,
value: true,
},
__sentry_original__: {
enumerable: false,
value: fn,
},
});
// Restore original function name (not all browsers allow that)
try {
const descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name') as PropertyDescriptor;
if (descriptor.configurable) {
Object.defineProperty(sentryWrapped, 'name', {
get(): string {
return fn.name;
},
});
}
// eslint-disable-next-line no-empty
} catch (_oO) {}
return sentryWrapped;
}
/**
* All properties the report dialog supports
*/
export interface ReportDialogOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
eventId?: string;
dsn?: DsnLike;
user?: {
email?: string;
name?: string;
};
lang?: string;
title?: string;
subtitle?: string;
subtitle2?: string;
labelName?: string;
labelEmail?: string;
labelComments?: string;
labelClose?: string;
labelSubmit?: string;
errorGeneric?: string;
errorFormEntry?: string;
successMessage?: string;
/** Callback after reportDialog showed up */
onLoad?(): void;
}
/**
* Injects the Report Dialog script
* @hidden
*/
export function injectReportDialog(options: ReportDialogOptions = {}): void {
if (!global.document) {
return;
}
if (!options.eventId) {
logger.error(`Missing eventId option in showReportDialog call`);
return;
}
if (!options.dsn) {
logger.error(`Missing dsn option in showReportDialog call`);
return;
}
const script = global.document.createElement('script');
script.async = true;
script.src = new API(options.dsn).getReportDialogEndpoint(options);
if (options.onLoad) {
// eslint-disable-next-line @typescript-eslint/unbound-method
script.onload = options.onLoad;
}
const injectionPoint = global.document.head || global.document.body;
if (injectionPoint) {
injectionPoint.appendChild(script);
}
}