akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
42 lines (41 loc) • 1.97 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { explainedByReaderGone } from "../core/stdio.js";
import { EXIT_CODES } from "./shared.js";
import { setUsageError } from "./usage-kinds.js";
/**
* Route an error that escaped every command's handling (an unhandled rejection or an uncaught exception) through the
* JSON error shape, record its cause for usage, and exit INTERNAL (70). A reader that closed early is not a failure.
*/
export function installCrashHandlers() {
process.on("unhandledRejection", (reason) => {
if (explainedByReaderGone(reason))
return;
const err = reason instanceof Error ? reason : new Error(String(reason));
console.error(JSON.stringify({
ok: false,
error: `Unhandled rejection: ${err.message}`,
code: "UNHANDLED_REJECTION",
hint: "Re-run with AKM_DEBUG=1 for a stack trace, or report at https://github.com/itlackey/akm/issues with the failing command.",
}, null, 2));
if (process.env.AKM_DEBUG === "1" && err.stack)
console.error(err.stack);
setUsageError(reason, "unhandled");
process.exit(EXIT_CODES.INTERNAL);
});
process.on("uncaughtException", (err) => {
if (explainedByReaderGone(err))
return;
console.error(JSON.stringify({
ok: false,
error: `Uncaught exception: ${err.message}`,
code: "UNCAUGHT_EXCEPTION",
hint: "Re-run with AKM_DEBUG=1 for a stack trace, or report at https://github.com/itlackey/akm/issues with the failing command.",
}, null, 2));
if (process.env.AKM_DEBUG === "1" && err.stack)
console.error(err.stack);
setUsageError(err, "uncaught");
process.exit(EXIT_CODES.INTERNAL);
});
}