@sentry/browser
Version:
Official Sentry SDK for browsers
121 lines (120 loc) • 4.17 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { eventFromUnknownInput } from "../eventbuilder.js";
import { shouldIgnoreOnError } from "../helpers.js";
import { UNKNOWN_FUNCTION, addGlobalErrorInstrumentationHandler, addGlobalUnhandledRejectionInstrumentationHandler, captureEvent, debug, defineIntegration, getClient, isPrimitive, isString, stripDataUrlContent } from "@sentry/core";
import { getLocationHref } from "@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 = defineIntegration(_globalHandlersIntegration);
function _installGlobalOnErrorHandler(client) {
addGlobalErrorInstrumentationHandler((data) => {
const { stackParser, attachStacktrace } = getOptions();
if (getClient() !== client || shouldIgnoreOnError(data)) return;
const { msg, url, line, column, error } = data;
const event = _enhanceEventWithInitialFrame(eventFromUnknownInput(stackParser, error || msg, void 0, attachStacktrace, false), url, line, column);
event.level = "error";
captureEvent(event, {
originalException: error,
mechanism: {
handled: false,
type: "auto.browser.global_handlers.onerror"
}
});
});
}
function _installGlobalOnUnhandledRejectionHandler(client) {
addGlobalUnhandledRejectionInstrumentationHandler((e) => {
const { stackParser, attachStacktrace } = getOptions();
if (getClient() !== client || shouldIgnoreOnError()) return;
const error = _getUnhandledRejectionError(e);
const event = isPrimitive(error) ? _eventFromRejectionWithPrimitive(error) : eventFromUnknownInput(stackParser, error, void 0, attachStacktrace, true);
event.level = "error";
captureEvent(event, {
originalException: error,
mechanism: {
handled: false,
type: "auto.browser.global_handlers.onunhandledrejection"
}
});
});
}
/**
*
*/
function _getUnhandledRejectionError(error) {
if (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) ?? getLocationHref(),
function: UNKNOWN_FUNCTION,
in_app: true
});
return event;
}
function globalHandlerLog(type) {
DEBUG_BUILD && debug.log(`Global Handler attached: ${type}`);
}
function getOptions() {
return getClient()?.getOptions() || {
stackParser: () => [],
attachStacktrace: false
};
}
function getFilenameFromUrl(url) {
if (!isString(url) || url.length === 0) return;
if (url.startsWith("data:")) return `<${stripDataUrlContent(url, false)}>`;
return url;
}
export { _enhanceEventWithInitialFrame, _eventFromRejectionWithPrimitive, _getUnhandledRejectionError, globalHandlersIntegration };
//# sourceMappingURL=globalhandlers.js.map