refactor: share cli runtime error handling
This commit is contained in:
@@ -7,6 +7,7 @@ import { defaultRuntime } from "../../runtime.js";
|
||||
import { formatDocsLink } from "../../terminal/links.js";
|
||||
import { colorize, isRich, theme } from "../../terminal/theme.js";
|
||||
import { withProgress } from "../progress.js";
|
||||
import { runCommandWithRuntime } from "../cli-utils.js";
|
||||
import { callGatewayCli, gatewayCallOpts } from "./call.js";
|
||||
import type { GatewayDiscoverOpts } from "./discover.js";
|
||||
import {
|
||||
@@ -41,6 +42,14 @@ function styleHealthChannelLine(line: string, rich: boolean): string {
|
||||
return line;
|
||||
}
|
||||
|
||||
function runGatewayCommand(action: () => Promise<void>, label?: string) {
|
||||
return runCommandWithRuntime(defaultRuntime, action, (err) => {
|
||||
const message = String(err);
|
||||
defaultRuntime.error(label ? `${label}: ${message}` : message);
|
||||
defaultRuntime.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
export function registerGatewayCli(program: Command) {
|
||||
const gateway = addGatewayRunCommand(
|
||||
program
|
||||
@@ -67,7 +76,7 @@ export function registerGatewayCli(program: Command) {
|
||||
.argument("<method>", "Method name (health/status/system-presence/cron.*)")
|
||||
.option("--params <json>", "JSON object string for params", "{}")
|
||||
.action(async (method, opts) => {
|
||||
try {
|
||||
await runGatewayCommand(async () => {
|
||||
const params = JSON.parse(String(opts.params ?? "{}"));
|
||||
const result = await callGatewayCli(method, opts, params);
|
||||
if (opts.json) {
|
||||
@@ -79,10 +88,7 @@ export function registerGatewayCli(program: Command) {
|
||||
`${colorize(rich, theme.heading, "Gateway call")}: ${colorize(rich, theme.muted, String(method))}`,
|
||||
);
|
||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||
} catch (err) {
|
||||
defaultRuntime.error(`Gateway call failed: ${String(err)}`);
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
}, "Gateway call failed");
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -91,7 +97,7 @@ export function registerGatewayCli(program: Command) {
|
||||
.command("health")
|
||||
.description("Fetch Gateway health")
|
||||
.action(async (opts) => {
|
||||
try {
|
||||
await runGatewayCommand(async () => {
|
||||
const result = await callGatewayCli("health", opts);
|
||||
if (opts.json) {
|
||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||
@@ -110,10 +116,7 @@ export function registerGatewayCli(program: Command) {
|
||||
defaultRuntime.log(styleHealthChannelLine(line, rich));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
defaultRuntime.error(String(err));
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -129,12 +132,9 @@ export function registerGatewayCli(program: Command) {
|
||||
.option("--timeout <ms>", "Overall probe budget in ms", "3000")
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts) => {
|
||||
try {
|
||||
await runGatewayCommand(async () => {
|
||||
await gatewayStatusCommand(opts, defaultRuntime);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(String(err));
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
gateway
|
||||
@@ -145,7 +145,7 @@ export function registerGatewayCli(program: Command) {
|
||||
.option("--timeout <ms>", "Per-command timeout in ms", "2000")
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts: GatewayDiscoverOpts) => {
|
||||
try {
|
||||
await runGatewayCommand(async () => {
|
||||
const timeoutMs = parseDiscoverTimeoutMs(opts.timeout, 2000);
|
||||
const beacons = await withProgress(
|
||||
{
|
||||
@@ -200,9 +200,6 @@ export function registerGatewayCli(program: Command) {
|
||||
defaultRuntime.log(line);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
defaultRuntime.error(`gateway discover failed: ${String(err)}`);
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
}, "gateway discover failed");
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user