





















@@ -1,5 +1,6 @@
11import type { Command } from "commander";
22import type { HealthSummary } from "../../commands/health.js";
3+import { formatGatewayTransportErrorJson } from "../../gateway/call.js";
34import type { CostUsageSummary } from "../../infra/session-cost-usage.js";
45import type {
56DiagnosticStabilityBundle,
@@ -97,8 +98,16 @@ function loadDaemonStatusGatherModule() {
9798return daemonStatusGatherModuleLoader.load();
9899}
99100100-function runGatewayCommand(action: () => Promise<void>, label?: string) {
101+function runGatewayCommand(action: () => Promise<void>, label?: string, opts?: { json?: boolean }) {
101102return runCommandWithRuntime(defaultRuntime, action, (err) => {
103+if (opts?.json) {
104+const payload = formatGatewayTransportErrorJson(err);
105+if (payload) {
106+defaultRuntime.writeJson(payload);
107+defaultRuntime.exit(1);
108+return;
109+}
110+}
102111const message = String(err);
103112defaultRuntime.error(label ? `${label}: ${message}` : message);
104113defaultRuntime.exit(1);
@@ -459,30 +468,34 @@ export function registerGatewayCli(program: Command) {
459468.command("health")
460469.description("Fetch Gateway health")
461470.action(async (opts, command) => {
462-await runGatewayCommand(async () => {
463-const rpcOpts = resolveGatewayRpcOptions(opts, command);
464-const [{ formatHealthChannelLines }, { styleHealthChannelLine }] = await Promise.all([
465-loadGatewayHealthModule(),
466-loadHealthStyleModule(),
467-]);
468-const result = await callGatewayCli("health", rpcOpts);
469-if (rpcOpts.json) {
470-defaultRuntime.writeJson(result);
471-return;
472-}
473-const rich = isRich();
474-const obj: Record<string, unknown> = result && typeof result === "object" ? result : {};
475-const durationMs = typeof obj.durationMs === "number" ? obj.durationMs : null;
476-defaultRuntime.log(colorize(rich, theme.heading, "Gateway Health"));
477-defaultRuntime.log(
478-`${colorize(rich, theme.success, "OK")}${durationMs != null ? ` (${durationMs}ms)` : ""}`,
479-);
480-if (obj.channels && typeof obj.channels === "object") {
481-for (const line of formatHealthChannelLines(obj as HealthSummary)) {
482-defaultRuntime.log(styleHealthChannelLine(line, rich));
471+await runGatewayCommand(
472+async () => {
473+const rpcOpts = resolveGatewayRpcOptions(opts, command);
474+const [{ formatHealthChannelLines }, { styleHealthChannelLine }] = await Promise.all([
475+loadGatewayHealthModule(),
476+loadHealthStyleModule(),
477+]);
478+const result = await callGatewayCli("health", rpcOpts);
479+if (rpcOpts.json) {
480+defaultRuntime.writeJson(result);
481+return;
483482}
484-}
485-});
483+const rich = isRich();
484+const obj: Record<string, unknown> = result && typeof result === "object" ? result : {};
485+const durationMs = typeof obj.durationMs === "number" ? obj.durationMs : null;
486+defaultRuntime.log(colorize(rich, theme.heading, "Gateway Health"));
487+defaultRuntime.log(
488+`${colorize(rich, theme.success, "OK")}${durationMs != null ? ` (${durationMs}ms)` : ""}`,
489+);
490+if (obj.channels && typeof obj.channels === "object") {
491+for (const line of formatHealthChannelLines(obj as HealthSummary)) {
492+defaultRuntime.log(styleHealthChannelLine(line, rich));
493+}
494+}
495+},
496+undefined,
497+{ json: Boolean(opts.json) },
498+);
486499}),
487500);
488501此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。