@sentry/browser
Version:
Official Sentry SDK for browsers
150 lines (125 loc) • 3.86 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var core = require('@sentry/core');
var utils = require('@sentry/utils');
let ignoreOnError = 0;
/**
* @hidden
*/
function shouldIgnoreOnError() {
return ignoreOnError > 0;
}
/**
* @hidden
*/
function ignoreNextOnError() {
// 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
*/
function wrap(
fn,
options
= {},
before,
) {
// for future readers what this does is wrap a function and then create
// a bi-directional wrapping between them.
//
// example: wrapped = wrap(original);
// original.__sentry_wrapped__ -> wrapped
// wrapped.__sentry_original__ -> original
if (typeof fn !== 'function') {
return fn;
}
try {
// if we're dealing with a function that was previously wrapped, return
// the original wrapper.
var wrapper = fn.__sentry_wrapped__;
if (wrapper) {
return wrapper;
}
// We don't wanna wrap it twice
if (utils.getOriginalFunction(fn)) {
return fn;
}
} 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;
}
var sentryWrapped = function () {
var args = Array.prototype.slice.call(arguments);
try {
if (before && typeof before === 'function') {
before.apply(this, arguments);
}
var wrappedArguments = args.map((arg) => wrap(arg, options));
// 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();
core.withScope((scope) => {
scope.addEventProcessor((event) => {
if (options.mechanism) {
utils.addExceptionTypeValue(event, undefined, undefined);
utils.addExceptionMechanism(event, options.mechanism);
}
event.extra = {
...event.extra,
arguments: args,
};
return event;
});
core.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 (var property in fn) {
if (Object.prototype.hasOwnProperty.call(fn, property)) {
sentryWrapped[property] = fn[property];
}
}
} catch (_oO) {}
// Signal that this function has been wrapped/filled already
// for both debugging and to prevent it to being wrapped/filled twice
utils.markFunctionWrapped(sentryWrapped, fn);
utils.addNonEnumerableProperty(fn, '__sentry_wrapped__', sentryWrapped);
// Restore original function name (not all browsers allow that)
try {
var descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name') ;
if (descriptor.configurable) {
Object.defineProperty(sentryWrapped, 'name', {
get() {
return fn.name;
},
});
}
} catch (_oO) {}
return sentryWrapped;
}
/**
* All properties the report dialog supports
*/
exports.ignoreNextOnError = ignoreNextOnError;
exports.shouldIgnoreOnError = shouldIgnoreOnError;
exports.wrap = wrap;
//# sourceMappingURL=helpers.js.map