@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
120 lines (117 loc) • 4.03 kB
JavaScript
import { addNonEnumerableProperty } from './object.js';
import { withRandomSafeContext, safeMathRandom } from './randomSafeContext.js';
import { snipLine } from './string.js';
import { GLOBAL_OBJ } from './worldwide.js';
function getCrypto() {
const gbl = GLOBAL_OBJ;
return gbl.crypto || gbl.msCrypto;
}
let emptyUuid;
function getRandomByte() {
return safeMathRandom() * 16;
}
function uuid4(crypto = getCrypto()) {
try {
if (crypto?.randomUUID) {
return withRandomSafeContext(() => crypto.randomUUID()).replace(/-/g, "");
}
} catch {
}
if (!emptyUuid) {
emptyUuid = "10000000100040008000" + 1e11;
}
return emptyUuid.replace(
/[018]/g,
(c) => (
// eslint-disable-next-line no-bitwise
(c ^ (getRandomByte() & 15) >> c / 4).toString(16)
)
);
}
function getFirstException(event) {
return event.exception?.values?.[0];
}
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>";
}
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";
}
}
function addExceptionMechanism(event, newMechanism) {
const firstException = getFirstException(event);
if (!firstException) {
return;
}
const defaultMechanism = { type: "generic", handled: true };
const currentMechanism = firstException.mechanism;
firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };
if (newMechanism && "data" in newMechanism) {
const mergedData = { ...currentMechanism?.data, ...newMechanism.data };
firstException.mechanism.data = mergedData;
}
}
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);
}
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]
};
}
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));
}
function checkOrSetAlreadyCaught(exception) {
if (isAlreadyCaptured(exception)) {
return true;
}
try {
addNonEnumerableProperty(exception, "__sentry_captured__", true);
} catch {
}
return false;
}
function isAlreadyCaptured(exception) {
try {
return exception.__sentry_captured__;
} catch {
}
}
export { addContextToFrame, addExceptionMechanism, addExceptionTypeValue, checkOrSetAlreadyCaught, getEventDescription, isAlreadyCaptured, parseSemver, uuid4 };
//# sourceMappingURL=misc.js.map