@sentry/browser
Version:
Official Sentry SDK for browsers
200 lines (199 loc) • 8.33 kB
JavaScript
import { _INTERNAL_enhanceErrorWithSentryInfo, addExceptionMechanism, addExceptionTypeValue, extractExceptionKeysForMessage, getClient, isDOMError, isDOMException, isError, isErrorEvent, isEvent, isParameterizedString, isPlainObject, normalizeToSize, resolvedSyncPromise } from "@sentry/core";
// src/eventbuilder.ts
/**
* This function creates an exception from a JavaScript Error
*/
function exceptionFromError(stackParser, ex) {
const frames = parseStackFrames(stackParser, ex);
const exception = {
type: extractType(ex),
value: extractMessage(ex)
};
if (frames.length) exception.stacktrace = { frames };
if (exception.type === void 0 && exception.value === "") exception.value = "Unrecoverable error caught";
return exception;
}
function eventFromPlainObject(stackParser, exception, syntheticException, isUnhandledRejection) {
const normalizeDepth = getClient()?.getOptions().normalizeDepth;
const errorFromProp = getErrorPropertyFromObject(exception);
const extra = { __serialized__: normalizeToSize(exception, normalizeDepth) };
if (errorFromProp) return {
exception: { values: [exceptionFromError(stackParser, errorFromProp)] },
extra
};
const event = {
exception: { values: [{
type: isEvent(exception) ? exception.constructor.name : isUnhandledRejection ? "UnhandledRejection" : "Error",
value: getNonErrorObjectExceptionValue(exception, { isUnhandledRejection })
}] },
extra
};
if (syntheticException) {
const frames = parseStackFrames(stackParser, syntheticException);
if (frames.length) event.exception.values[0].stacktrace = { frames };
}
return event;
}
function eventFromError(stackParser, ex) {
return { exception: { values: [exceptionFromError(stackParser, ex)] } };
}
/** Parses stack frames from an error */
function parseStackFrames(stackParser, ex) {
const stacktrace = ex.stacktrace || ex.stack || "";
const skipLines = getSkipFirstStackStringLines(ex);
const framesToPop = getPopFirstTopFrames(ex);
try {
return stackParser(stacktrace, skipLines, framesToPop);
} catch {}
return [];
}
const reactMinifiedRegexp = /Minified React error #\d+;/i;
/**
* Certain known React errors contain links that would be falsely
* parsed as frames. This function check for these errors and
* returns number of the stack string lines to skip.
*/
function getSkipFirstStackStringLines(ex) {
if (ex && reactMinifiedRegexp.test(ex.message)) return 1;
return 0;
}
/**
* If error has `framesToPop` property, it means that the
* creator tells us the first x frames will be useless
* and should be discarded. Typically error from wrapper function
* which don't point to the actual location in the developer's code.
*
* Example: https://github.com/zertosh/invariant/blob/master/invariant.js#L46
*/
function getPopFirstTopFrames(ex) {
if (typeof ex.framesToPop === "number") return ex.framesToPop;
return 0;
}
function isWebAssemblyException(exception) {
if (typeof WebAssembly !== "undefined" && typeof WebAssembly.Exception !== "undefined") return exception instanceof WebAssembly.Exception;
else return false;
}
/**
* Extracts from errors what we use as the exception `type` in error events.
*
* Usually, this is the `name` property on Error objects but WASM errors need to be treated differently.
*/
function extractType(ex) {
const name = ex?.name;
if (!name && isWebAssemblyException(ex)) return ex.message && Array.isArray(ex.message) && ex.message.length == 2 ? ex.message[0] : "WebAssembly.Exception";
return name;
}
/**
* There are cases where stacktrace.message is an Event object
* https://github.com/getsentry/sentry-javascript/issues/1949
* In this specific case we try to extract stacktrace.message.error.message
*/
function extractMessage(ex) {
const message = ex?.message;
if (isWebAssemblyException(ex)) {
if (Array.isArray(ex.message) && ex.message.length == 2) return ex.message[1];
return "wasm exception";
}
if (!message) return "No error message";
if (message.error && typeof message.error.message === "string") return _INTERNAL_enhanceErrorWithSentryInfo(message.error);
return _INTERNAL_enhanceErrorWithSentryInfo(ex);
}
/**
* Creates an {@link Event} from all inputs to `captureException` and non-primitive inputs to `captureMessage`.
* @hidden
*/
function eventFromException(stackParser, exception, hint, attachStacktrace) {
const event = eventFromUnknownInput(stackParser, exception, hint?.syntheticException || void 0, attachStacktrace);
addExceptionMechanism(event);
event.level = "error";
if (hint?.event_id) event.event_id = hint.event_id;
return resolvedSyncPromise(event);
}
/**
* Builds and Event from a Message
* @hidden
*/
function eventFromMessage(stackParser, message, level = "info", hint, attachStacktrace) {
const event = eventFromString(stackParser, message, hint?.syntheticException || void 0, attachStacktrace);
event.level = level;
if (hint?.event_id) event.event_id = hint.event_id;
return resolvedSyncPromise(event);
}
/**
* @hidden
*/
function eventFromUnknownInput(stackParser, exception, syntheticException, attachStacktrace, isUnhandledRejection) {
let event;
if (isErrorEvent(exception) && exception.error) return eventFromError(stackParser, exception.error);
if (isDOMError(exception) || isDOMException(exception)) {
const domException = exception;
if ("stack" in exception) {
event = eventFromError(stackParser, exception);
const firstException = event.exception?.values?.[0];
if (attachStacktrace && syntheticException && firstException && !firstException.stacktrace) {
const frames = parseStackFrames(stackParser, syntheticException);
if (frames.length) {
firstException.stacktrace = { frames };
addExceptionMechanism(event, { synthetic: true });
}
}
} else {
const name = domException.name || (isDOMError(domException) ? "DOMError" : "DOMException");
const message = domException.message ? `${name}: ${domException.message}` : name;
event = eventFromString(stackParser, message, syntheticException, attachStacktrace);
addExceptionTypeValue(event, message);
}
return event;
}
if (isError(exception)) return eventFromError(stackParser, exception);
if (isPlainObject(exception) || isEvent(exception)) {
event = eventFromPlainObject(stackParser, exception, syntheticException, isUnhandledRejection);
addExceptionMechanism(event, { synthetic: true });
return event;
}
const stringifiedException = String(exception);
event = eventFromString(stackParser, stringifiedException, syntheticException, attachStacktrace);
addExceptionTypeValue(event, stringifiedException, void 0);
addExceptionMechanism(event, { synthetic: true });
return event;
}
function eventFromString(stackParser, message, syntheticException, attachStacktrace) {
const event = {};
if (attachStacktrace && syntheticException) {
const frames = parseStackFrames(stackParser, syntheticException);
if (frames.length) event.exception = { values: [{
value: message,
stacktrace: { frames }
}] };
addExceptionMechanism(event, { synthetic: true });
}
if (isParameterizedString(message)) {
const { __sentry_template_string__, __sentry_template_values__ } = message;
event.logentry = {
message: __sentry_template_string__,
params: __sentry_template_values__
};
return event;
}
event.message = message;
return event;
}
function getNonErrorObjectExceptionValue(exception, { isUnhandledRejection }) {
const keys = extractExceptionKeysForMessage(exception);
const captureType = isUnhandledRejection ? "promise rejection" : "exception";
if (isErrorEvent(exception)) return `Event \`ErrorEvent\` captured as ${captureType} with message \`${exception.message}\``;
if (isEvent(exception)) return `Event \`${getObjectClassName(exception)}\` (type=${exception.type}) captured as ${captureType}`;
return `Object captured as ${captureType} with keys: ${keys}`;
}
function getObjectClassName(obj) {
try {
const prototype = Object.getPrototypeOf(obj);
return prototype ? prototype.constructor.name : void 0;
} catch {}
}
/** If a plain object has a property that is an `Error`, return this error. */
function getErrorPropertyFromObject(obj) {
return Object.values(obj).find(isError);
}
export { eventFromException, eventFromMessage, eventFromUnknownInput, exceptionFromError, extractMessage, extractType };
//# sourceMappingURL=eventbuilder.js.map