refactor: share cli runtime error handling

This commit is contained in:
Peter Steinberger committed 2026-01-19 00:52:31 +00:00
1 parent c532d161c4
commit 1fec41b3df
16 files changed
+288 -452

No files matched your search

@@ -6,8 +6,22 @@ import { setVerbose } from "../../globals.js";
import { defaultRuntime } from "../../runtime.js";
import { formatDocsLink } from "../../terminal/links.js";
import { theme } from "../../terminal/theme.js";
import { runCommandWithRuntime } from "../cli-utils.js";
import { parsePositiveIntOrUndefined } from "./helpers.js";
import { ensureConfigReady } from "./config-guard.js";
function resolveVerbose(opts: { verbose?: boolean; debug?: boolean }): boolean {
return Boolean(opts.verbose || opts.debug);
}
function parseTimeoutMs(timeout: unknown): number | null | undefined {
const parsed = parsePositiveIntOrUndefined(timeout);
if (timeout !== undefined && parsed === undefined) {
defaultRuntime.error("--timeout must be a positive integer (milliseconds)");
defaultRuntime.exit(1);
return null;
}
return parsed;
}
export function registerStatusHealthSessionsCommands(program: Command) {
program
@@ -38,16 +52,13 @@ Examples:
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/status", "docs.clawd.bot/cli/status")}\n`,
)
.action(async (opts) => {
await ensureConfigReady({ runtime: defaultRuntime, migrateState: false });
const verbose = Boolean(opts.verbose || opts.debug);
const verbose = resolveVerbose(opts);
setVerbose(verbose);
const timeout = parsePositiveIntOrUndefined(opts.timeout);
if (opts.timeout !== undefined && timeout === undefined) {
defaultRuntime.error("--timeout must be a positive integer (milliseconds)");
defaultRuntime.exit(1);
const timeout = parseTimeoutMs(opts.timeout);
if (timeout === null) {
return;
}
try {
await runCommandWithRuntime(defaultRuntime, async () => {
await statusCommand(
{
json: Boolean(opts.json),
@@ -59,10 +70,7 @@ Examples:
},
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
program
@@ -78,16 +86,13 @@ Examples:
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/health", "docs.clawd.bot/cli/health")}\n`,
)
.action(async (opts) => {
await ensureConfigReady({ runtime: defaultRuntime, migrateState: false });
const verbose = Boolean(opts.verbose || opts.debug);
const verbose = resolveVerbose(opts);
setVerbose(verbose);
const timeout = parsePositiveIntOrUndefined(opts.timeout);
if (opts.timeout !== undefined && timeout === undefined) {
defaultRuntime.error("--timeout must be a positive integer (milliseconds)");
defaultRuntime.exit(1);
const timeout = parseTimeoutMs(opts.timeout);
if (timeout === null) {
return;
}
try {
await runCommandWithRuntime(defaultRuntime, async () => {
await healthCommand(
{
json: Boolean(opts.json),
@@ -96,10 +101,7 @@ Examples:
},
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
program
@@ -126,7 +128,6 @@ Shows token usage per session when the agent reports it; set agents.defaults.con
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/sessions", "docs.clawd.bot/cli/sessions")}\n`,
)
.action(async (opts) => {
await ensureConfigReady({ runtime: defaultRuntime, migrateState: false });
setVerbose(Boolean(opts.verbose));
await sessionsCommand(
{