refactor: share cli runtime error handling

This commit is contained in:
Peter Steinberger
2026-01-19 00:52:17 +00:00
parent c532d161c4
commit 1fec41b3df
16 changed files with 288 additions and 452 deletions

View File

@@ -14,6 +14,7 @@ import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js";
import { runChannelLogin, runChannelLogout } from "./channel-auth.js";
import { runCommandWithRuntime } from "./cli-utils.js";
import { hasExplicitOptions } from "./command-options.js";
const optionNamesAdd = [
@@ -44,6 +45,17 @@ const optionNamesAdd = [
const optionNamesRemove = ["channel", "account", "delete"] as const;
function runChannelsCommand(action: () => Promise<void>) {
return runCommandWithRuntime(defaultRuntime, action);
}
function runChannelsCommandWithDanger(action: () => Promise<void>, label: string) {
return runCommandWithRuntime(defaultRuntime, action, (err) => {
defaultRuntime.error(danger(`${label}: ${String(err)}`));
defaultRuntime.exit(1);
});
}
export function registerChannelsCli(program: Command) {
const channelNames = listChannelPlugins()
.map((plugin) => plugin.id)
@@ -66,12 +78,9 @@ export function registerChannelsCli(program: Command) {
.option("--no-usage", "Skip model provider usage/quota snapshots")
.option("--json", "Output JSON", false)
.action(async (opts) => {
try {
await runChannelsCommand(async () => {
await channelsListCommand(opts, defaultRuntime);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -81,12 +90,9 @@ export function registerChannelsCli(program: Command) {
.option("--timeout <ms>", "Timeout in ms", "10000")
.option("--json", "Output JSON", false)
.action(async (opts) => {
try {
await runChannelsCommand(async () => {
await channelsStatusCommand(opts, defaultRuntime);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -98,12 +104,9 @@ export function registerChannelsCli(program: Command) {
.option("--timeout <ms>", "Timeout in ms", "10000")
.option("--json", "Output JSON", false)
.action(async (opts) => {
try {
await runChannelsCommand(async () => {
await channelsCapabilitiesCommand(opts, defaultRuntime);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -115,7 +118,7 @@ export function registerChannelsCli(program: Command) {
.option("--kind <kind>", "Target kind (auto|user|group)", "auto")
.option("--json", "Output JSON", false)
.action(async (entries, opts) => {
try {
await runChannelsCommand(async () => {
await channelsResolveCommand(
{
channel: opts.channel as string | undefined,
@@ -126,10 +129,7 @@ export function registerChannelsCli(program: Command) {
},
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -139,12 +139,9 @@ export function registerChannelsCli(program: Command) {
.option("--lines <n>", "Number of lines (default: 200)", "200")
.option("--json", "Output JSON", false)
.action(async (opts) => {
try {
await runChannelsCommand(async () => {
await channelsLogsCommand(opts, defaultRuntime);
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -174,13 +171,10 @@ export function registerChannelsCli(program: Command) {
.option("--initial-sync-limit <n>", "Matrix initial sync limit")
.option("--use-env", "Use env token (default account only)", false)
.action(async (opts, command) => {
try {
await runChannelsCommand(async () => {
const hasFlags = hasExplicitOptions(command, optionNamesAdd);
await channelsAddCommand(opts, defaultRuntime, { hasFlags });
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -190,13 +184,10 @@ export function registerChannelsCli(program: Command) {
.option("--account <id>", "Account id (default when omitted)")
.option("--delete", "Delete config entries (no prompt)", false)
.action(async (opts, command) => {
try {
await runChannelsCommand(async () => {
const hasFlags = hasExplicitOptions(command, optionNamesRemove);
await channelsRemoveCommand(opts, defaultRuntime, { hasFlags });
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
});
channels
@@ -206,7 +197,8 @@ export function registerChannelsCli(program: Command) {
.option("--account <id>", "Account id (accountId)")
.option("--verbose", "Verbose connection logs", false)
.action(async (opts) => {
try {
await runChannelsCommandWithDanger(
async () => {
await runChannelLogin(
{
channel: opts.channel as string | undefined,
@@ -215,10 +207,9 @@ export function registerChannelsCli(program: Command) {
},
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(danger(`Channel login failed: ${String(err)}`));
defaultRuntime.exit(1);
}
},
"Channel login failed",
);
});
channels
@@ -227,7 +218,8 @@ export function registerChannelsCli(program: Command) {
.option("--channel <channel>", "Channel alias (default: whatsapp)")
.option("--account <id>", "Account id (accountId)")
.action(async (opts) => {
try {
await runChannelsCommandWithDanger(
async () => {
await runChannelLogout(
{
channel: opts.channel as string | undefined,
@@ -235,9 +227,8 @@ export function registerChannelsCli(program: Command) {
},
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(danger(`Channel logout failed: ${String(err)}`));
defaultRuntime.exit(1);
}
},
"Channel logout failed",
);
});
}