Skip to content

Commit 4b8ca98

Browse files
authored
fix(gateway): omit internal class names from RPC failures (#124329)
* fix(gateway): drop internal error class names from operator output * test(cli): drop stale Error: prefixes from capability expectations * test(cli): align invalid-port output with canonical formatting * fix(errors): preserve primary structured codes * style(cli): format invalid-port expectation * test(cli): align shared error rendering expectations * fix(gateway): scope error code rendering to agent failures * test(errors): redact opt-in structured codes * refactor(errors): keep canonical formatter callback-safe * test(audit): drop stale Error: prefixes after formatter cleanup PR #124336 landed audit's gateway-error rendering while this branch was in flight, so its two new assertions were written against the prefixed output. The canonical formatter no longer emits the generic Error: prefix, so the expected strings are updated to match.
1 parent 44c5abb commit 4b8ca98

23 files changed

Lines changed: 168 additions & 60 deletions

‎packages/normalization-core/src/error-coercion.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ describe("formatErrorMessage", () => {
1616
it("walks and deduplicates Error cause chains while preserving codes", () => {
1717
const root = Object.assign(new Error("socket closed"), { code: "ECONNRESET" });
1818
const inner = new Error("request failed", { cause: root });
19-
const outer = new Error("request failed", { cause: inner });
19+
const outer = Object.assign(new Error("request failed", { cause: inner }), {
20+
code: "REQUEST_FAILED",
21+
});
2022

2123
expect(format(outer)).toBe("request failed | socket closed | ECONNRESET");
24+
expect(formatErrorMessage(outer, { includeCode: true, redact: keepText })).toBe(
25+
"request failed | REQUEST_FAILED | socket closed | ECONNRESET",
26+
);
2227
});
2328

2429
it("formats status/code records and structured non-Error causes", () => {

‎packages/normalization-core/src/error-coercion.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Structural formatting stays policy-free. Core and memory-host adapters intentionally inject
22
// owner-specific redactors; bypassing them would weaken redaction and break one-argument APIs.
33
export type FormatErrorMessageOptions = {
4+
includeCode?: boolean;
45
redact: (text: string) => string;
56
};
67

@@ -86,6 +87,12 @@ export function formatErrorMessage(value: unknown, options: FormatErrorMessageOp
8687
formatted += ` | ${message}`;
8788
seenMessages.add(message);
8889
};
90+
if (options.includeCode) {
91+
const code = readProperty(value, "code");
92+
if (typeof code === "string" || typeof code === "number") {
93+
appendCauseMessage(String(code));
94+
}
95+
}
8996
while (cause && !seen.has(cause)) {
9097
seen.add(cause);
9198
if (cause instanceof Error) {

‎src/agents/live-model-errors.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("live model error helpers", () => {
1414
expect(isModelNotFoundErrorMessage("The model gpt-foo does not exist.")).toBe(true);
1515
expect(
1616
isModelNotFoundErrorMessage(
17-
"FailoverError: The selected model was not found by the provider. Check the model id or choose a different model.",
17+
"The selected model was not found by the provider. Check the model id or choose a different model.",
1818
),
1919
).toBe(true);
2020
expect(isModelNotFoundErrorMessage('{"code":404,"message":"model not found"}')).toBe(true);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ describe("capability cli", () => {
16701670
runCapability("image", "describe", "--file", "photo.jpg", "--json"),
16711671
).rejects.toThrow("exit 1");
16721672
expect(runtimeErrorMessages()).toEqual([
1673-
`Error: No description returned for image: ${path.resolve("photo.jpg")}`,
1673+
`No description returned for image: ${path.resolve("photo.jpg")}`,
16741674
]);
16751675
});
16761676

@@ -1877,7 +1877,7 @@ describe("capability cli", () => {
18771877
),
18781878
).rejects.toThrow("exit 1");
18791879
expect(mocks.runtime.error).toHaveBeenCalledWith(
1880-
"Error: --output-format must be one of png, jpeg, or webp",
1880+
"--output-format must be one of png, jpeg, or webp",
18811881
);
18821882

18831883
mocks.runtime.error.mockClear();
@@ -1893,7 +1893,7 @@ describe("capability cli", () => {
18931893
),
18941894
).rejects.toThrow("exit 1");
18951895
expect(mocks.runtime.error).toHaveBeenCalledWith(
1896-
"Error: --openai-background must be one of transparent, opaque, or auto",
1896+
"--openai-background must be one of transparent, opaque, or auto",
18971897
);
18981898

18991899
mocks.runtime.error.mockClear();
@@ -1909,7 +1909,7 @@ describe("capability cli", () => {
19091909
),
19101910
).rejects.toThrow("exit 1");
19111911
expect(mocks.runtime.error).toHaveBeenCalledWith(
1912-
"Error: --background must be one of transparent, opaque, or auto",
1912+
"--background must be one of transparent, opaque, or auto",
19131913
);
19141914

19151915
mocks.runtime.error.mockClear();
@@ -1925,7 +1925,7 @@ describe("capability cli", () => {
19251925
),
19261926
).rejects.toThrow("exit 1");
19271927
expect(mocks.runtime.error).toHaveBeenCalledWith(
1928-
"Error: --quality must be one of low, medium, high, or auto",
1928+
"--quality must be one of low, medium, high, or auto",
19291929
);
19301930

19311931
mocks.runtime.error.mockClear();
@@ -1941,7 +1941,7 @@ describe("capability cli", () => {
19411941
),
19421942
).rejects.toThrow("exit 1");
19431943
expect(mocks.runtime.error).toHaveBeenCalledWith(
1944-
"Error: --openai-moderation must be one of low or auto",
1944+
"--openai-moderation must be one of low or auto",
19451945
);
19461946
});
19471947

@@ -2662,7 +2662,7 @@ describe("capability cli", () => {
26622662
runCapability("audio", "transcribe", "--file", "memo.m4a", "--json"),
26632663
).rejects.toThrow("exit 1");
26642664
expect(runtimeErrorMessages()).toEqual([
2665-
`Error: No transcript returned for audio: ${path.resolve("memo.m4a")}`,
2665+
`No transcript returned for audio: ${path.resolve("memo.m4a")}`,
26662666
]);
26672667
});
26682668

@@ -2691,7 +2691,7 @@ describe("capability cli", () => {
26912691
await expect(
26922692
runCapability("audio", "transcribe", "--file", "memo.m4a", "--json"),
26932693
).rejects.toThrow("exit 1");
2694-
expect(runtimeErrorMessages()).toEqual(["Error: Audio transcription response missing text"]);
2694+
expect(runtimeErrorMessages()).toEqual(["Audio transcription response missing text"]);
26952695
});
26962696

26972697
it("forwards transcription prompt and language hints", async () => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ describe("runCommandWithRuntime", () => {
5555
);
5656

5757
expect(messages).toHaveLength(1);
58-
expect(messages[0]).toContain("TypeError: fetch failed");
58+
expect(messages[0]).toContain("fetch failed");
59+
expect(messages[0]).not.toContain("TypeError:");
5960
expect(messages[0]).toContain("invalid onRequestStart method");
6061
expect(messages[0]).toContain("UND_ERR_INVALID_ARG");
6162
expect(exits).toEqual([1]);

‎src/cli/cli-utils.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ export async function withManager<T>(params: {
3232
}
3333
}
3434

35-
function formatCommandRuntimeError(err: unknown): string {
36-
if (err instanceof Error) {
37-
return formatErrorMessage(new Error(String(err), { cause: err.cause }));
38-
}
39-
return formatErrorMessage(err);
40-
}
41-
4235
export async function runCommandWithRuntime(
4336
runtime: { error: (message: string) => void; exit: (code: number) => void },
4437
action: () => Promise<void>,
@@ -51,7 +44,7 @@ export async function runCommandWithRuntime(
5144
onError(err);
5245
return;
5346
}
54-
runtime.error(formatCommandRuntimeError(err));
47+
runtime.error(formatErrorMessage(err));
5548
runtime.exit(1);
5649
}
5750
}

‎src/cli/gateway-cli.coverage.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,20 @@ describe("gateway-cli coverage", () => {
156156
await expectGatewayExit(["gateway", "call", "health", "--timeout", "1000ms", "--json"]);
157157

158158
expect(callGateway).not.toHaveBeenCalled();
159-
expect(runtimeErrors.join("\n")).toContain("Gateway call failed: Error: Invalid --timeout");
159+
expect(runtimeErrors.join("\n")).toContain("Gateway call failed: Invalid --timeout");
160+
});
161+
162+
it("renders gateway request failures without the client error class in human mode", async () => {
163+
const error = Object.assign(new Error("invalid audit.activity.list params"), {
164+
name: "GatewayClientRequestError",
165+
gatewayCode: "INVALID_REQUEST",
166+
});
167+
callGateway.mockRejectedValueOnce(error);
168+
169+
await expectGatewayExit(["gateway", "call", "audit.activity.list"]);
170+
171+
expect(runtimeErrors.join("\n")).toContain("invalid audit.activity.list params");
172+
expect(runtimeErrors.join("\n")).not.toContain("GatewayClientRequestError");
160173
});
161174

162175
it("registers gateway probe and routes to gatewayStatusCommand", async () => {

‎src/cli/gateway-cli/register.option-collisions.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ describe("gateway register option collisions", () => {
329329

330330
expect(callGatewayCli).not.toHaveBeenCalled();
331331
expect(defaultRuntime.error).toHaveBeenCalledWith(
332-
"Gateway call failed: Error: Use either --url or --port, not both.",
332+
"Gateway call failed: Use either --url or --port, not both.",
333333
);
334334
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
335335
});

‎src/cli/gateway-cli/register.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Command } from "commander";
55
import { formatDocsLink } from "../../../packages/terminal-core/src/links.js";
66
import { colorize, isRich, theme } from "../../../packages/terminal-core/src/theme.js";
77
import type { HealthSummary } from "../../commands/health.js";
8+
import { formatErrorMessage } from "../../infra/errors.js";
89
import type { CostUsageSummary } from "../../infra/session-cost-usage.js";
910
import type {
1011
DiagnosticStabilityBundle,
@@ -151,7 +152,7 @@ async function runGatewayCommand(
151152
return;
152153
}
153154
}
154-
const message = String(err);
155+
const message = formatErrorMessage(err);
155156
defaultRuntime.error(label ? `${label}: ${message}` : message);
156157
defaultRuntime.exit(1);
157158
}

‎src/cli/help-exit.process.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,7 @@ describe("rejected CLI process state isolation", () => {
391391
pristineHome: true,
392392
});
393393

394-
expect(result.stderr).toContain(
395-
"Error: --gateway-port must be an integer between 1 and 65535.",
396-
);
394+
expect(result.stderr).toContain("--gateway-port must be an integer between 1 and 65535.");
397395
await expect(fs.access(path.join(result.root, `.openclaw-${profile}`))).rejects.toMatchObject({
398396
code: "ENOENT",
399397
});

0 commit comments

Comments
 (0)