UNPKG

@sentry/browser

Version:
124 lines (123 loc) • 3.83 kB
import { GLOBAL_OBJ, addExceptionMechanism, addExceptionTypeValue, addNonEnumerableProperty, captureException, filterCollectedUrl, getOriginalFunction, markFunctionWrapped, withScope } from "@sentry/core"; import { getLocationHref } from "@sentry/browser-utils"; // src/helpers.ts const WINDOW = 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 (getOriginalFunction(fn)) return fn; } catch { return fn; } const sentryWrapped = function(...args) { GLOBAL_OBJ._sentryWrappedDepth = (GLOBAL_OBJ._sentryWrappedDepth || 0) + 1; try { const wrappedArguments = args.map((arg) => wrap(arg, options)); return fn.apply(this, wrappedArguments); } catch (ex) { ignoreNextOnError(); withScope((scope) => { scope.addEventProcessor((event) => { if (options.mechanism) { addExceptionTypeValue(event, void 0, void 0); addExceptionMechanism(event, options.mechanism); } event.extra = { ...event.extra, arguments: args }; return event; }); captureException(ex); }); throw ex; } finally { GLOBAL_OBJ._sentryWrappedDepth = (GLOBAL_OBJ._sentryWrappedDepth || 0) - 1; } }; try { for (const property in fn) if (Object.prototype.hasOwnProperty.call(fn, property)) sentryWrapped[property] = fn[property]; } catch {} markFunctionWrapped(sentryWrapped, fn); 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 = getLocationHref(); const { referrer } = WINDOW.document || {}; const { userAgent } = WINDOW.navigator || {}; return { url, headers: { ...referrer && { Referer: filterCollectedUrl(referrer, client) }, ...userAgent && { "User-Agent": userAgent } } }; } export { WINDOW, getHttpRequestData, ignoreNextOnError, ignoreNextOnErrorMatching, shouldIgnoreOnError, wrap }; //# sourceMappingURL=helpers.js.map