Skip to content

Commit 036e635

Browse files
authored
fix: preserve Gateway error reasons in text logs (#130645)
Print the existing formatted and redacted RPC error instead of a generic reachability claim. Preserve JSON output, selected-target details, fallback/recovery, auth and exit behavior. Verified with real loopback rejection and built CLI before/after proof. Closes #130616.
1 parent a678ed6 commit 036e635

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎docs/cli/logs.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ profile uses `openclaw-YYYY-MM-DD.log`, while named profiles use
6464

6565
In `--json` mode, invalid `--port`, `--limit`, `--interval`, or `--max-bytes` values and conflicting `--url`/`--port` options produce the standard CLI failure envelope on stdout: `{"ok":false,"error":{"type":"cli_error","message":"..."}}`. Terminal log-fetch failures instead emit `{"type":"error",...}` on stderr. Both exit with status `1`.
6666

67+
In text mode, terminal log-fetch failures print the redacted error reason, selected Gateway connection details, and a doctor hint on stderr. A received RPC rejection is shown as the error, rather than reported as a Gateway reachability failure.
68+
6769
## Related
6870

6971
- [Logging overview](/logging)

‎src/cli/logs-cli.port.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe("logs local port selection", () => {
117117
);
118118

119119
it.each(["text", "json"])(
120-
"reports the selected port when the RPC fails in %s mode",
120+
"reports the rejection reason and selected port when the RPC fails in %s mode",
121121
async (mode) => {
122122
await withLogsGateway({ denied: true }, async ({ port, requests, stderr }) => {
123123
const args = [
@@ -130,12 +130,16 @@ describe("logs local port selection", () => {
130130
await expect(runLogs(args)).rejects.toBeInstanceOf(ExitError);
131131
expect(requests).toEqual(["connect", "logs.tail"]);
132132
const output = stderr.join("");
133+
expect(output).toContain("logs unavailable for this client");
133134
if (mode === "json") {
134135
expect(JSON.parse(output)).toMatchObject({
135136
type: "error",
137+
message: "Gateway not reachable. Is it running and accessible?",
138+
error: "logs unavailable for this client",
136139
details: { url: `ws://127.0.0.1:${port}` },
137140
});
138141
} else {
142+
expect(output).not.toContain("Gateway not reachable");
139143
expect(output).toContain(`Gateway target: ws://127.0.0.1:${port}`);
140144
}
141145
});

‎src/cli/logs-cli.test.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,9 @@ describe("logs cli", () => {
890890

891891
expect(readConfiguredLogTail).not.toHaveBeenCalled();
892892
expect((stderrWrites.join("").match(/gateway disconnected/g) ?? []).length).toBe(8);
893-
expect(stderrWrites.join("")).toContain("Gateway not reachable");
893+
expect(stderrWrites.join("")).toContain(
894+
"gateway closed (1006 abnormal closure): abnormal closure",
895+
);
894896
expect(stdoutWrites.join("")).not.toContain("local fallback line");
895897
expect(exitSpy).toHaveBeenCalledWith(1);
896898
});
@@ -914,7 +916,9 @@ describe("logs cli", () => {
914916
await runLogsCli(["logs", "--follow", "--url", "ws://127.0.0.1:18789"]);
915917

916918
expect((stderrWrites.join("").match(/gateway disconnected/g) ?? []).length).toBe(8);
917-
expect(stderrWrites.join("")).toContain("Gateway not reachable");
919+
expect(stderrWrites.join("")).toContain(
920+
"gateway closed (1006 abnormal closure): abnormal closure",
921+
);
918922
expect(exitSpy).toHaveBeenCalledWith(1);
919923
});
920924

@@ -1018,7 +1022,9 @@ describe("logs cli", () => {
10181022
await runLogsCli(["logs", "--follow", "--url", "ws://127.0.0.1:18789"]);
10191023

10201024
expect(stderrWrites.join("")).not.toContain("gateway disconnected");
1021-
expect(stderrWrites.join("")).toContain("Gateway not reachable");
1025+
expect(stderrWrites.join("")).toContain(
1026+
"gateway closed (1008 policy violation): pairing required",
1027+
);
10221028
expect(exitSpy).toHaveBeenCalledWith(1);
10231029
});
10241030

@@ -1038,11 +1044,11 @@ describe("logs cli", () => {
10381044
await runLogsCli(["logs", "--follow", "--url", "ws://127.0.0.1:18789"]);
10391045

10401046
expect(stderrWrites.join("")).not.toContain("gateway disconnected");
1041-
expect(stderrWrites.join("")).toContain("Gateway not reachable");
1047+
expect(stderrWrites.join("")).toContain("gateway closed (4001 unauthorized): unauthorized");
10421048
expect(exitSpy).toHaveBeenCalledWith(1);
10431049
});
10441050

1045-
it("redacts credential-bearing Gateway URLs from JSON errors", async () => {
1051+
it.each(["text", "json"])("redacts Gateway URLs in %s errors", async (mode) => {
10461052
const rawUrl =
10471053
"wss://user:[email protected]/ws?token=secret&key=api-key&X-Amz-Signature=signed";
10481054
buildGatewayConnectionDetails.mockReturnValueOnce({
@@ -1054,9 +1060,10 @@ describe("logs cli", () => {
10541060
const stderrWrites = captureStderrWrites();
10551061
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
10561062

1057-
await runLogsCli(["logs", "--json", "--url", rawUrl]);
1063+
await runLogsCli(["logs", mode === "json" ? "--json" : "--plain", "--url", rawUrl]);
10581064

10591065
const stderr = stderrWrites.join("");
1066+
expect(stderr).toContain("failed to connect to");
10601067
expect(stderr).toContain("gateway.example/ws");
10611068
expect(stderr).not.toContain("password");
10621069
expect(stderr).not.toContain("secret");
@@ -1126,7 +1133,9 @@ describe("logs cli", () => {
11261133

11271134
expect(readConfiguredLogTail).not.toHaveBeenCalled();
11281135
expect(stdoutWrites.join("")).not.toContain("local fallback line");
1129-
expect(stderrWrites.join("")).toContain("Gateway not reachable");
1136+
expect(stderrWrites.join("")).toContain(
1137+
"gateway closed (1000 normal closure): no close reason",
1138+
);
11301139
expect(exitSpy).toHaveBeenCalledWith(1);
11311140
});
11321141

‎src/cli/logs-cli.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ async function emitGatewayError(
507507
return;
508508
}
509509

510-
if (!errorLine(colorize(rich, theme.error, message))) {
510+
if (!errorLine(colorize(rich, theme.error, errorText))) {
511511
return;
512512
}
513513
if (!errorLine(details.message)) {

0 commit comments

Comments
 (0)