UNPKG

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