@sentry/browser
Version:
Official Sentry SDK for browsers
124 lines (123 loc) • 4.5 kB
JavaScript
const require_debug_build = require("../debug-build.js");
const require_eventbuilder = require("../eventbuilder.js");
const require_helpers = require("../helpers.js");
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@sentry/browser-utils");
// src/integrations/globalhandlers.ts
const INTEGRATION_NAME = "GlobalHandlers";
const _globalHandlersIntegration = ((options = {}) => {
const _options = {
onerror: true,
onunhandledrejection: true,
...options
};
return {
name: INTEGRATION_NAME,
setupOnce() {
Error.stackTraceLimit = 50;
},
setup(client) {
if (_options.onerror) {
_installGlobalOnErrorHandler(client);
globalHandlerLog("onerror");
}
if (_options.onunhandledrejection) {
_installGlobalOnUnhandledRejectionHandler(client);
globalHandlerLog("onunhandledrejection");
}
}
};
});
const globalHandlersIntegration = (0, _sentry_core.defineIntegration)(_globalHandlersIntegration);
function _installGlobalOnErrorHandler(client) {
(0, _sentry_core.addGlobalErrorInstrumentationHandler)((data) => {
const { stackParser, attachStacktrace } = getOptions();
if ((0, _sentry_core.getClient)() !== client || require_helpers.shouldIgnoreOnError(data)) return;
const { msg, url, line, column, error } = data;
const event = _enhanceEventWithInitialFrame(require_eventbuilder.eventFromUnknownInput(stackParser, error || msg, void 0, attachStacktrace, false), url, line, column);
event.level = "error";
(0, _sentry_core.captureEvent)(event, {
originalException: error,
mechanism: {
handled: false,
type: "auto.browser.global_handlers.onerror"
}
});
});
}
function _installGlobalOnUnhandledRejectionHandler(client) {
(0, _sentry_core.addGlobalUnhandledRejectionInstrumentationHandler)((e) => {
const { stackParser, attachStacktrace } = getOptions();
if ((0, _sentry_core.getClient)() !== client || require_helpers.shouldIgnoreOnError()) return;
const error = _getUnhandledRejectionError(e);
const event = (0, _sentry_core.isPrimitive)(error) ? _eventFromRejectionWithPrimitive(error) : require_eventbuilder.eventFromUnknownInput(stackParser, error, void 0, attachStacktrace, true);
event.level = "error";
(0, _sentry_core.captureEvent)(event, {
originalException: error,
mechanism: {
handled: false,
type: "auto.browser.global_handlers.onunhandledrejection"
}
});
});
}
/**
*
*/
function _getUnhandledRejectionError(error) {
if ((0, _sentry_core.isPrimitive)(error)) return error;
try {
if ("reason" in error) return error.reason;
if ("detail" in error && "reason" in error.detail) return error.detail.reason;
} catch {}
return error;
}
/**
* Create an event from a promise rejection where the `reason` is a primitive.
*
* @param reason: The `reason` property of the promise rejection
* @returns An Event object with an appropriate `exception` value
*/
function _eventFromRejectionWithPrimitive(reason) {
return { exception: { values: [{
type: "UnhandledRejection",
value: `Non-Error promise rejection captured with value: ${String(reason)}`
}] } };
}
/**
* Adds a frame built from the error location when the event has none.
*/
function _enhanceEventWithInitialFrame(event, url, lineno, colno) {
const e = event.exception = event.exception || {};
const ev = e.values = e.values || [];
const ev0 = ev[0] = ev[0] || {};
const ev0s = ev0.stacktrace = ev0.stacktrace || {};
const ev0sf = ev0s.frames = ev0s.frames || [];
if (ev0sf.length === 0) ev0sf.push({
colno,
lineno,
filename: getFilenameFromUrl(url) ?? (0, _sentry_browser_utils.getLocationHref)(),
function: _sentry_core.UNKNOWN_FUNCTION,
in_app: true
});
return event;
}
function globalHandlerLog(type) {
require_debug_build.DEBUG_BUILD && _sentry_core.debug.log(`Global Handler attached: ${type}`);
}
function getOptions() {
return (0, _sentry_core.getClient)()?.getOptions() || {
stackParser: () => [],
attachStacktrace: false
};
}
function getFilenameFromUrl(url) {
if (!(0, _sentry_core.isString)(url) || url.length === 0) return;
if (url.startsWith("data:")) return `<${(0, _sentry_core.stripDataUrlContent)(url, false)}>`;
return url;
}
exports._enhanceEventWithInitialFrame = _enhanceEventWithInitialFrame;
exports._eventFromRejectionWithPrimitive = _eventFromRejectionWithPrimitive;
exports._getUnhandledRejectionError = _getUnhandledRejectionError;
exports.globalHandlersIntegration = globalHandlersIntegration;
//# sourceMappingURL=globalhandlers.js.map