Skip to content

Commit 69433a6

Browse files
committed
fix(cli): render Gateway health JSON failures
1 parent 79c2d77 commit 69433a6

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

‎src/cli/gateway-cli/health-route.test.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,31 @@ describe("runGatewayHealthJsonRoute", () => {
8080
);
8181
});
8282

83-
it("formats local config resolution failures", async () => {
83+
it("leaves local config resolution failures to the root CLI renderer", async () => {
8484
const runtime = createRuntime();
8585
const error = new Error("config unavailable");
8686
const callGateway = vi.fn();
8787

88-
await runGatewayHealthJsonRoute(
89-
{
90-
rpc: { json: true, timeout: "10000" },
91-
localPortOverride: 19083,
92-
},
93-
runtime as never,
94-
{
95-
callGateway,
96-
readNonObservingHealthConfig: vi.fn(async () => {
97-
throw error;
98-
}),
99-
},
100-
);
88+
await expect(
89+
runGatewayHealthJsonRoute(
90+
{
91+
rpc: { json: true, timeout: "10000" },
92+
localPortOverride: 19083,
93+
},
94+
runtime as never,
95+
{
96+
callGateway,
97+
readNonObservingHealthConfig: vi.fn(async () => {
98+
throw error;
99+
}),
100+
},
101+
),
102+
).rejects.toBe(error);
101103

102104
expect(callGateway).not.toHaveBeenCalled();
103105
expect(runtime.writeJson).not.toHaveBeenCalled();
104-
expect(runtime.error).toHaveBeenCalledWith(error.message);
105-
expect(runtime.exit).toHaveBeenCalledWith(1);
106+
expect(runtime.error).not.toHaveBeenCalled();
107+
expect(runtime.exit).not.toHaveBeenCalled();
106108
});
107109

108110
it("preserves structured transport errors", async () => {

‎src/cli/gateway-cli/health-route.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Route-first machine-readable Gateway health command.
2-
import { formatErrorMessage } from "../../infra/errors.js";
32
import { type RuntimeEnv, writeRuntimeJson } from "../../runtime.js";
43

54
type GatewayHealthRpcOpts = Parameters<
@@ -65,9 +64,7 @@ export async function runGatewayHealthJsonRoute(
6564
);
6665
} catch (error) {
6766
if (!rpc) {
68-
runtime.error(formatErrorMessage(error));
69-
runtime.exit(1);
70-
return;
67+
throw error;
7168
}
7269
const [healthModule, callModule] = await Promise.all([
7370
deps.emitReachableGatewayAuthDiagnostic && deps.readNonObservingHealthConfig

‎test/cli-json-stdout.e2e.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ describe("cli json stdout contract", () => {
211211
message: "Use --agent or --all-agents, not both",
212212
tty: true,
213213
},
214+
{
215+
name: "routed Gateway health config failure in JSON mode",
216+
args: ["gateway", "health", "--port", "29793", "--json"],
217+
message: "AUTOQA_ROUTE_CONFIG_READ_FAILURE",
218+
configReadFailure: true,
219+
},
220+
{
221+
name: "Gateway health config failure in human mode",
222+
args: ["gateway", "health", "--port", "29793"],
223+
message: "AUTOQA_ROUTE_CONFIG_READ_FAILURE",
224+
configReadFailure: true,
225+
human: true,
226+
},
227+
{
228+
name: "Gateway health config failure through forced Commander",
229+
args: ["gateway", "health", "--port", "29793", "--json"],
230+
message: "AUTOQA_ROUTE_CONFIG_READ_FAILURE",
231+
configReadFailure: true,
232+
commander: true,
233+
},
234+
{
235+
name: "routed Gateway health config failure with dual TTYs",
236+
args: ["gateway", "health", "--port", "29793", "--json"],
237+
message: "AUTOQA_ROUTE_CONFIG_READ_FAILURE",
238+
configReadFailure: true,
239+
tty: true,
240+
},
214241
{
215242
name: "specialized explicit Gateway authentication failure",
216243
args: ["gateway", "call", "system-presence", "--url", "ws://127.0.0.1:29793", "--json"],
@@ -226,6 +253,18 @@ describe("cli json stdout contract", () => {
226253
[
227254
'import net from "node:net";',
228255
`net.Socket.prototype.connect = function () { throw new Error(${JSON.stringify(gatewayError)}); };`,
256+
...("configReadFailure" in testCase
257+
? [
258+
'import fs from "node:fs";',
259+
"const originalExistsSync = fs.existsSync;",
260+
"fs.existsSync = function (target, ...args) {",
261+
' if (String(target) === process.env.OPENCLAW_CONFIG_PATH && new Error().stack?.includes("readNonObservingHealthConfig")) {',
262+
` throw new Error(${JSON.stringify(testCase.message)});`,
263+
" }",
264+
" return originalExistsSync.call(this, target, ...args);",
265+
"};",
266+
]
267+
: []),
229268
...("tty" in testCase
230269
? [
231270
'Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true });',
@@ -269,7 +308,11 @@ describe("cli json stdout contract", () => {
269308
ok: false,
270309
error: { type: "cli_error", message: testCase.message },
271310
});
272-
expect(result.stderr).not.toContain(testCase.message);
311+
if ("configReadFailure" in testCase && !("commander" in testCase)) {
312+
expect(result.stderr).toContain(testCase.message);
313+
} else {
314+
expect(result.stderr).not.toContain(testCase.message);
315+
}
273316
}
274317
if ("tty" in testCase) {
275318
expect(result.stderr).toContain("\u001B[?25h");

0 commit comments

Comments
 (0)