@sentry/browser
Version:
Official Sentry SDK for browsers
129 lines (128 loc) • 4.06 kB
JavaScript
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@sentry/browser-utils");
// src/helpers.ts
const WINDOW = _sentry_core.GLOBAL_OBJ;
let ignoreOnError = 0;
const ignoredErrorReports = [];
/**
* @hidden
*/
function shouldIgnoreOnError(data) {
if (ignoreOnError > 0) return true;
const index = data ? ignoredErrorReports.findIndex((report) => isSameErrorReport(report, data)) : -1;
if (index === -1) return false;
ignoredErrorReports.splice(index, 1);
return true;
}
/**
* Skips the next `onerror` report that matches `report`, once. A report that
* never arrives is forgotten after the current task.
*
* @hidden
*/
function ignoreNextOnErrorMatching(report) {
ignoredErrorReports.push(report);
setTimeout(() => {
const index = ignoredErrorReports.indexOf(report);
if (index !== -1) ignoredErrorReports.splice(index, 1);
});
}
function isSameErrorReport(a, b) {
return a.msg === b.msg && a.url === b.url && a.line === b.line && a.column === b.column;
}
/**
* @hidden
*/
function ignoreNextOnError() {
ignoreOnError++;
setTimeout(() => {
ignoreOnError--;
});
}
/**
* Instruments the given function and sends an event to Sentry every time the
* function throws an exception.
*
* @param fn A function to wrap. It is generally safe to pass an unbound function, because the returned wrapper always
* has a correct `this` context.
* @returns The wrapped function.
* @hidden
*/
function wrap(fn, options = {}) {
function isFunction(fn) {
return typeof fn === "function";
}
if (!isFunction(fn)) return fn;
try {
if (Object.prototype.hasOwnProperty.call(fn, "__sentry_wrapped__")) {
const wrapper = fn.__sentry_wrapped__;
if (typeof wrapper === "function") return wrapper;
else return fn;
}
if ((0, _sentry_core.getOriginalFunction)(fn)) return fn;
} catch {
return fn;
}
const sentryWrapped = function(...args) {
_sentry_core.GLOBAL_OBJ._sentryWrappedDepth = (_sentry_core.GLOBAL_OBJ._sentryWrappedDepth || 0) + 1;
try {
const wrappedArguments = args.map((arg) => wrap(arg, options));
return fn.apply(this, wrappedArguments);
} catch (ex) {
ignoreNextOnError();
(0, _sentry_core.withScope)((scope) => {
scope.addEventProcessor((event) => {
if (options.mechanism) {
(0, _sentry_core.addExceptionTypeValue)(event, void 0, void 0);
(0, _sentry_core.addExceptionMechanism)(event, options.mechanism);
}
event.extra = {
...event.extra,
arguments: args
};
return event;
});
(0, _sentry_core.captureException)(ex);
});
throw ex;
} finally {
_sentry_core.GLOBAL_OBJ._sentryWrappedDepth = (_sentry_core.GLOBAL_OBJ._sentryWrappedDepth || 0) - 1;
}
};
try {
for (const property in fn) if (Object.prototype.hasOwnProperty.call(fn, property)) sentryWrapped[property] = fn[property];
} catch {}
(0, _sentry_core.markFunctionWrapped)(sentryWrapped, fn);
(0, _sentry_core.addNonEnumerableProperty)(fn, "__sentry_wrapped__", sentryWrapped);
try {
if (Object.getOwnPropertyDescriptor(sentryWrapped, "name").configurable) Object.defineProperty(sentryWrapped, "name", { get() {
return fn.name;
} });
} catch {}
return sentryWrapped;
}
/**
* Get HTTP request data from the current page.
*
* The `Referer` header has `dataCollection.urlQueryParams` of `client` applied. `url` is unfiltered, so
* callers must pass it through `filterCollectedUrl`.
*/
function getHttpRequestData(client) {
const url = (0, _sentry_browser_utils.getLocationHref)();
const { referrer } = WINDOW.document || {};
const { userAgent } = WINDOW.navigator || {};
return {
url,
headers: {
...referrer && { Referer: (0, _sentry_core.filterCollectedUrl)(referrer, client) },
...userAgent && { "User-Agent": userAgent }
}
};
}
exports.WINDOW = WINDOW;
exports.getHttpRequestData = getHttpRequestData;
exports.ignoreNextOnError = ignoreNextOnError;
exports.ignoreNextOnErrorMatching = ignoreNextOnErrorMatching;
exports.shouldIgnoreOnError = shouldIgnoreOnError;
exports.wrap = wrap;
//# sourceMappingURL=helpers.js.map