@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
143 lines (142 loc) • 5.27 kB
JavaScript
import { isError, isErrorEvent, isParameterizedString, isPlainObject, isPrimitive } from "./is.js";
import { addExceptionMechanism, addExceptionTypeValue } from "./misc.js";
import { normalizeToSize, stringifyValue } from "./normalize.js";
import { extractExceptionKeysForMessage } from "./object.js";
import { stringify } from "./string.js";
// src/utils/eventbuilder.ts
/**
* Extracts stack frames from the error.stack string
*/
function parseStackFrames(stackParser, error) {
return stackParser(error.stack || "", 1);
}
function hasSentryFetchUrlHost(error) {
return isError(error) && "__sentry_fetch_url_host__" in error && typeof error.__sentry_fetch_url_host__ === "string";
}
/**
* Enhances the error message with the hostname for better Sentry error reporting.
* This allows third-party packages to still match on the original error message,
* while Sentry gets the enhanced version with context.
*
* Only used internally
* @hidden
*/
function _enhanceErrorWithSentryInfo(error) {
const message = errorMessageToString(error.message);
if (hasSentryFetchUrlHost(error)) return `${message} (${error.__sentry_fetch_url_host__})`;
return message;
}
function errorMessageToString(message) {
if (typeof message === "string") return message;
if (message == null) return "";
if (isPrimitive(message)) return String(message);
if (isPlainObject(message) || Array.isArray(message)) {
const json = stringify(message, "");
if (json) return json;
}
return stringifyValue(void 0, message);
}
/**
* Extracts stack frames from the error and builds a Sentry Exception
*/
function exceptionFromError(stackParser, error) {
const exception = {
type: error.name || error.constructor.name,
value: _enhanceErrorWithSentryInfo(error)
};
const frames = parseStackFrames(stackParser, error);
if (frames.length) exception.stacktrace = { frames };
return exception;
}
/** If a plain object has a property that is an `Error`, return this error. */
function getErrorPropertyFromObject(obj) {
for (const prop in obj) if (Object.prototype.hasOwnProperty.call(obj, prop)) {
const value = obj[prop];
if (isError(value)) return value;
}
}
function getMessageForObject(exception) {
if ("name" in exception && typeof exception.name === "string") {
let message = `'${exception.name}' captured as exception`;
if ("message" in exception && typeof exception.message === "string") message += ` with message '${exception.message}'`;
return message;
} else if ("message" in exception && typeof exception.message === "string") return exception.message;
const keys = extractExceptionKeysForMessage(exception);
if (isErrorEvent(exception)) return `Event \`ErrorEvent\` captured as exception with message \`${exception.message}\``;
const className = getObjectClassName(exception);
return `${className && className !== "Object" ? `'${className}'` : "Object"} captured as exception with keys: ${keys}`;
}
function getObjectClassName(obj) {
try {
const prototype = Object.getPrototypeOf(obj);
return prototype ? prototype.constructor.name : void 0;
} catch {}
}
function getException(client, mechanism, exception, hint) {
if (isError(exception)) return [exception, void 0];
mechanism.synthetic = true;
if (isPlainObject(exception)) {
const normalizeDepth = client?.getOptions().normalizeDepth;
const extras = { ["__serialized__"]: normalizeToSize(exception, normalizeDepth) };
const errorFromProp = getErrorPropertyFromObject(exception);
if (errorFromProp) return [errorFromProp, extras];
const message = getMessageForObject(exception);
const ex = hint?.syntheticException || new Error(message);
ex.message = message;
return [ex, extras];
}
const ex = hint?.syntheticException || new Error(exception);
ex.message = `${exception}`;
return [ex, void 0];
}
/**
* Builds and Event from a Exception
* @hidden
*/
function eventFromUnknownInput(client, stackParser, exception, hint) {
const mechanism = hint?.data && hint.data.mechanism || {
handled: true,
type: "generic"
};
const [ex, extras] = getException(client, mechanism, exception, hint);
const event = { exception: { values: [exceptionFromError(stackParser, ex)] } };
if (extras) event.extra = extras;
addExceptionTypeValue(event, void 0, void 0);
addExceptionMechanism(event, mechanism);
return {
...event,
event_id: hint?.event_id
};
}
/**
* Builds and Event from a Message
* @hidden
*/
function eventFromMessage(stackParser, message, level = "info", hint, attachStacktrace) {
const event = {
event_id: hint?.event_id,
level
};
if (attachStacktrace && hint?.syntheticException) {
const frames = parseStackFrames(stackParser, hint.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;
}
export { _enhanceErrorWithSentryInfo, eventFromMessage, eventFromUnknownInput, exceptionFromError, parseStackFrames };
//# sourceMappingURL=eventbuilder.js.map