UNPKG

5.55 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { captureException, getReportDialogEndpoint, withScope } from '@sentry/core';
3import { addExceptionMechanism, addExceptionTypeValue, addNonEnumerableProperty, getGlobalObject, getOriginalFunction, logger, markFunctionWrapped, } from '@sentry/utils';
4import { IS_DEBUG_BUILD } from './flags';
5var global = getGlobalObject();
6var ignoreOnError = 0;
7/**
8 * @hidden
9 */
10export function shouldIgnoreOnError() {
11 return ignoreOnError > 0;
12}
13/**
14 * @hidden
15 */
16export function ignoreNextOnError() {
17 // onerror should trigger before setTimeout
18 ignoreOnError += 1;
19 setTimeout(function () {
20 ignoreOnError -= 1;
21 });
22}
23/**
24 * Instruments the given function and sends an event to Sentry every time the
25 * function throws an exception.
26 *
27 * @param fn A function to wrap.
28 * @returns The wrapped function.
29 * @hidden
30 */
31export function wrap(fn, options, before) {
32 // for future readers what this does is wrap a function and then create
33 // a bi-directional wrapping between them.
34 //
35 // example: wrapped = wrap(original);
36 // original.__sentry_wrapped__ -> wrapped
37 // wrapped.__sentry_original__ -> original
38 if (options === void 0) { options = {}; }
39 if (typeof fn !== 'function') {
40 return fn;
41 }
42 try {
43 // if we're dealing with a function that was previously wrapped, return
44 // the original wrapper.
45 var wrapper = fn.__sentry_wrapped__;
46 if (wrapper) {
47 return wrapper;
48 }
49 // We don't wanna wrap it twice
50 if (getOriginalFunction(fn)) {
51 return fn;
52 }
53 }
54 catch (e) {
55 // Just accessing custom props in some Selenium environments
56 // can cause a "Permission denied" exception (see raven-js#495).
57 // Bail on wrapping and return the function as-is (defers to window.onerror).
58 return fn;
59 }
60 /* eslint-disable prefer-rest-params */
61 // eslint-disable-next-line @typescript-eslint/no-explicit-any
62 var sentryWrapped = function () {
63 var args = Array.prototype.slice.call(arguments);
64 try {
65 if (before && typeof before === 'function') {
66 before.apply(this, arguments);
67 }
68 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
69 var wrappedArguments = args.map(function (arg) { return wrap(arg, options); });
70 // Attempt to invoke user-land function
71 // NOTE: If you are a Sentry user, and you are seeing this stack frame, it
72 // means the sentry.javascript SDK caught an error invoking your application code. This
73 // is expected behavior and NOT indicative of a bug with sentry.javascript.
74 return fn.apply(this, wrappedArguments);
75 }
76 catch (ex) {
77 ignoreNextOnError();
78 withScope(function (scope) {
79 scope.addEventProcessor(function (event) {
80 if (options.mechanism) {
81 addExceptionTypeValue(event, undefined, undefined);
82 addExceptionMechanism(event, options.mechanism);
83 }
84 event.extra = __assign(__assign({}, event.extra), { arguments: args });
85 return event;
86 });
87 captureException(ex);
88 });
89 throw ex;
90 }
91 };
92 /* eslint-enable prefer-rest-params */
93 // Accessing some objects may throw
94 // ref: https://github.com/getsentry/sentry-javascript/issues/1168
95 try {
96 for (var property in fn) {
97 if (Object.prototype.hasOwnProperty.call(fn, property)) {
98 sentryWrapped[property] = fn[property];
99 }
100 }
101 }
102 catch (_oO) { } // eslint-disable-line no-empty
103 // Signal that this function has been wrapped/filled already
104 // for both debugging and to prevent it to being wrapped/filled twice
105 markFunctionWrapped(sentryWrapped, fn);
106 addNonEnumerableProperty(fn, '__sentry_wrapped__', sentryWrapped);
107 // Restore original function name (not all browsers allow that)
108 try {
109 var descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name');
110 if (descriptor.configurable) {
111 Object.defineProperty(sentryWrapped, 'name', {
112 get: function () {
113 return fn.name;
114 },
115 });
116 }
117 // eslint-disable-next-line no-empty
118 }
119 catch (_oO) { }
120 return sentryWrapped;
121}
122/**
123 * Injects the Report Dialog script
124 * @hidden
125 */
126export function injectReportDialog(options) {
127 if (options === void 0) { options = {}; }
128 if (!global.document) {
129 return;
130 }
131 if (!options.eventId) {
132 IS_DEBUG_BUILD && logger.error('Missing eventId option in showReportDialog call');
133 return;
134 }
135 if (!options.dsn) {
136 IS_DEBUG_BUILD && logger.error('Missing dsn option in showReportDialog call');
137 return;
138 }
139 var script = global.document.createElement('script');
140 script.async = true;
141 script.src = getReportDialogEndpoint(options.dsn, options);
142 if (options.onLoad) {
143 // eslint-disable-next-line @typescript-eslint/unbound-method
144 script.onload = options.onLoad;
145 }
146 var injectionPoint = global.document.head || global.document.body;
147 if (injectionPoint) {
148 injectionPoint.appendChild(script);
149 }
150}
151//# sourceMappingURL=helpers.js.map
\No newline at end of file