one
Version:
One is a new React Framework that makes Vite serve both native and web.
106 lines (105 loc) • 2.67 kB
JavaScript
let userPrepare;
let depth = 0;
const restoreTrail = [];
function defaultFormat(error, stack) {
const name = error?.name ?? "Error";
const message = error?.message ?? "";
const head = `${name}: ${message}`;
try {
const lines = stack.map(frame => {
try {
return ` at ${frame}`;
} catch {
return " at <frame>";
}
});
return `${head}
${lines.join("\n")}`;
} catch {
return head;
}
}
function guardedPrepare(error, stack) {
if (depth > 0) return defaultFormat(error, stack);
if (typeof userPrepare !== "function") return defaultFormat(error, stack);
depth++;
try {
return userPrepare(error, stack);
} catch {
return defaultFormat(error, stack);
} finally {
depth--;
}
}
let installed = false;
function installPrepareStackTraceGuard() {
if (installed) return;
installed = true;
const existing = Error.prepareStackTrace;
if (typeof existing === "function" && existing !== guardedPrepare) {
userPrepare = existing;
}
Object.defineProperty(Error, "prepareStackTrace", {
configurable: true,
get() {
return guardedPrepare;
},
set(value) {
if (value === guardedPrepare) {
if (restoreTrail.length > 0) {
userPrepare = restoreTrail.pop();
}
return;
}
restoreTrail.push(userPrepare);
userPrepare = typeof value === "function" ? value : void 0;
}
});
}
function formatErrorSafely(err) {
const prevPrepare = Error.prepareStackTrace;
Error.prepareStackTrace = void 0;
try {
if (err instanceof Error) {
let stack = "";
try {
stack = err.stack ?? "";
} catch {}
if (stack) return stack;
const name = err.name || "Error";
const message = err.message || "";
return `${name}: ${message}`;
}
return String(err);
} catch {
try {
return String(err);
} catch {
return "<unprintable error>";
}
} finally {
Error.prepareStackTrace = prevPrepare;
}
}
function installSafeUncaughtHandler(label) {
installPrepareStackTraceGuard();
process.on("uncaughtException", err => {
try {
const formatted = formatErrorSafely(err);
process.stderr.write(`[${label}] uncaught exception
${formatted}
`);
} catch {}
process.exit(1);
});
process.on("unhandledRejection", reason => {
try {
const formatted = formatErrorSafely(reason);
process.stderr.write(`[${label}] unhandled rejection
${formatted}
`);
} catch {}
});
}
export { formatErrorSafely, installPrepareStackTraceGuard, installSafeUncaughtHandler };
//# sourceMappingURL=install-error-handlers.mjs.map