@sentry/browser
Version:
Official Sentry SDK for browsers
140 lines (137 loc) • 4.29 kB
JavaScript
import { defineIntegration, addGlobalErrorInstrumentationHandler, getClient, captureEvent, debug, addGlobalUnhandledRejectionInstrumentationHandler, isPrimitive, UNKNOWN_FUNCTION, getLocationHref, isString, stripDataUrlContent } from '@sentry/core/browser';
import { DEBUG_BUILD } from '../debug-build.js';
import { eventFromUnknownInput } from '../eventbuilder.js';
import { shouldIgnoreOnError } from '../helpers.js';
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()) {
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;
}
function _eventFromRejectionWithPrimitive(reason) {
return {
exception: {
values: [
{
type: "UnhandledRejection",
// String() is needed because the Primitive type includes symbols (which can't be automatically stringified)
value: `Non-Error promise rejection captured with value: ${String(reason)}`
}
]
}
};
}
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() {
const client = getClient();
const options = client?.getOptions() || {
stackParser: () => [],
attachStacktrace: false
};
return options;
}
function getFilenameFromUrl(url) {
if (!isString(url) || url.length === 0) {
return void 0;
}
if (url.startsWith("data:")) {
return `<${stripDataUrlContent(url, false)}>`;
}
return url;
}
export { _eventFromRejectionWithPrimitive, _getUnhandledRejectionError, globalHandlersIntegration };
//# sourceMappingURL=globalhandlers.js.map