UNPKG

@sentry/core

Version:
177 lines (176 loc) • 7.43 kB
import { addNonEnumerableProperty } from "./object.js"; import { safeMathRandom, withRandomSafeContext } from "./randomSafeContext.js"; import { snipLine } from "./string.js"; import { GLOBAL_OBJ } from "./worldwide.js"; // src/utils/misc.ts function getCrypto() { const gbl = GLOBAL_OBJ; return gbl.crypto || gbl.msCrypto; } let emptyUuid; function getRandomByte() { return safeMathRandom() * 16; } /** * UUID4 generator * @param crypto Object that provides the crypto API. * @returns string Generated UUID4. */ function uuid4(crypto = getCrypto()) { try { if (crypto?.randomUUID) return withRandomSafeContext(() => crypto.randomUUID()).replace(/-/g, ""); } catch {} if (!emptyUuid) emptyUuid = "10000000100040008000100000000000"; return emptyUuid.replace(/[018]/g, (c) => (c ^ (getRandomByte() & 15) >> c / 4).toString(16)); } function getFirstException(event) { return event.exception?.values?.[0]; } /** * Extracts either message or type+value from an event that can be used for user-facing logs * @returns event's description */ function getEventDescription(event) { const { message, event_id: eventId } = event; if (message) return message; const firstException = getFirstException(event); if (firstException) { if (firstException.type && firstException.value) return `${firstException.type}: ${firstException.value}`; return firstException.type || firstException.value || eventId || "<unknown>"; } return eventId || "<unknown>"; } /** * Adds exception values, type and value to an synthetic Exception. * @param event The event to modify. * @param value Value of the exception. * @param type Type of the exception. * @hidden */ function addExceptionTypeValue(event, value, type) { const exception = event.exception = event.exception || {}; const values = exception.values = exception.values || []; const firstException = values[0] = values[0] || {}; if (!firstException.value) firstException.value = value || ""; if (!firstException.type) firstException.type = type || "Error"; } /** * Adds exception mechanism data to a given event. Uses defaults if the second parameter is not passed. * * @param event The event to modify. * @param newMechanism Mechanism data to add to the event. * @hidden */ function addExceptionMechanism(event, newMechanism) { const firstException = getFirstException(event); if (!firstException) return; applyExceptionMechanism(firstException, newMechanism); } /** * Adds exception mechanism data to the originally captured (root) exception of a given event. * * @hidden */ function addExceptionMechanismToCapturedException(event, newMechanism) { const exceptions = event.exception?.values; const capturedException = exceptions?.find((exception) => exception.mechanism?.exception_id === 0) ?? exceptions?.[0]; if (!capturedException) return; applyExceptionMechanism(capturedException, newMechanism); } function applyExceptionMechanism(exception, newMechanism) { const defaultMechanism = { type: "generic", handled: true }; const currentMechanism = exception.mechanism; exception.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism }; if (newMechanism && "data" in newMechanism) exception.mechanism.data = { ...currentMechanism?.data, ...newMechanism.data }; } const SEMVER_REGEXP = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; function _parseInt(input) { return parseInt(input || "", 10); } /** * Parses input into a SemVer interface * @param input string representation of a semver version */ function parseSemver(input) { const match = input.match(SEMVER_REGEXP) || []; const major = _parseInt(match[1]); const minor = _parseInt(match[2]); const patch = _parseInt(match[3]); return { buildmetadata: match[5], major: isNaN(major) ? void 0 : major, minor: isNaN(minor) ? void 0 : minor, patch: isNaN(patch) ? void 0 : patch, prerelease: match[4] }; } /** * This function adds context (pre/post/line) lines to the provided frame * * @param lines string[] containing all lines * @param frame StackFrame that will be mutated * @param linesOfContext number of context lines we want to add pre/post */ function addContextToFrame(lines, frame, linesOfContext = 5) { if (frame.lineno === void 0) return; const maxLines = lines.length; const sourceLine = Math.max(Math.min(maxLines - 1, frame.lineno - 1), 0); frame.pre_context = lines.slice(Math.max(0, sourceLine - linesOfContext), sourceLine).map((line) => snipLine(line, 0)); const lineIndex = Math.min(maxLines - 1, sourceLine); frame.context_line = snipLine(lines[lineIndex], frame.colno || 0); frame.post_context = lines.slice(Math.min(sourceLine + 1, maxLines), sourceLine + 1 + linesOfContext).map((line) => snipLine(line, 0)); } /** * Checks whether or not we've already captured the given exception (note: not an identical exception - the very object * in question), and marks it captured if not. * * This is useful because it's possible for an error to get captured by more than one mechanism. After we intercept and * record an error, we rethrow it (assuming we've intercepted it before it's reached the top-level global handlers), so * that we don't interfere with whatever effects the error might have had were the SDK not there. At that point, because * the error has been rethrown, it's possible for it to bubble up to some other code we've instrumented. If it's not * caught after that, it will bubble all the way up to the global handlers (which of course we also instrument). This * function helps us ensure that even if we encounter the same error more than once, we only record it the first time we * see it. * * The client runs this check for every captured exception, also when `dedupeIntegration` is not active. * `dedupeIntegration` drops equal events that come from different error objects. * * Note: It will ignore primitives (always return `false` and not mark them as seen), as properties can't be set on * them. {@link: Object.objectify} can be used on exceptions to convert any that are primitives into their equivalent * object wrapper forms so that this check will always work. However, because we need to flag the exact object which * will get rethrown, and because that rethrowing happens outside of the event processing pipeline, the objectification * must be done before the exception captured. * * @param A thrown exception to check or flag as having been seen * @returns `true` if the exception has already been captured, `false` if not (with the side effect of marking it seen) */ function checkOrSetAlreadyCaught(exception) { if (isAlreadyCaptured(exception)) return true; try { addNonEnumerableProperty(exception, "__sentry_captured__", true); } catch {} return false; } /** * Checks whether we've already captured the given exception (note: not an identical exception - the very object). * It is considered already captured if it has the `__sentry_captured__` property set to `true`. * * @internal Only considered for internal usage */ function isAlreadyCaptured(exception) { try { return exception.__sentry_captured__; } catch {} } export { addContextToFrame, addExceptionMechanism, addExceptionMechanismToCapturedException, addExceptionTypeValue, checkOrSetAlreadyCaught, getEventDescription, isAlreadyCaptured, parseSemver, uuid4 }; //# sourceMappingURL=misc.js.map