Skip to content

Commit 599294b

Browse files
ly-wang19claude
andauthored
fix(acp-core): never return undefined from stringifyNonErrorCause (#96270)
`stringifyNonErrorCause` is typed `string`, but its `try` returned `JSON.stringify(value)`, which is `undefined` for functions, symbols, and undefined causes — leaking undefined to callers that format nested ACP runtime failures and expect a string. Fall back to a tag string when stringify yields undefined, matching the already-correct sibling at `src/infra/errors.ts`. Co-authored-by: ly-wang19 <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent bd43c36 commit 599294b

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Error-format helper tests cover the non-Error cause stringifier contract.
2+
import { describe, expect, it } from "vitest";
3+
import { stringifyNonErrorCause } from "./error-format.js";
4+
5+
describe("stringifyNonErrorCause", () => {
6+
it("returns a string for values JSON.stringify serializes to undefined", () => {
7+
// JSON.stringify(fn|symbol|undefined) is undefined; the `string`-typed helper must not leak it.
8+
expect(stringifyNonErrorCause(() => {})).toBe("[object Function]");
9+
expect(stringifyNonErrorCause(Symbol("x"))).toBe("[object Symbol]");
10+
expect(stringifyNonErrorCause(undefined)).toBe("[object Undefined]");
11+
});
12+
13+
it("stringifies ordinary scalar and object causes", () => {
14+
expect(stringifyNonErrorCause({ a: 1 })).toBe('{"a":1}');
15+
expect(stringifyNonErrorCause("hi")).toBe("hi");
16+
expect(stringifyNonErrorCause(42)).toBe("42");
17+
expect(stringifyNonErrorCause(null)).toBe("null");
18+
});
19+
});

‎packages/acp-core/src/error-format.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export function stringifyNonErrorCause(value: unknown): string {
7575
return String(value);
7676
}
7777
try {
78-
return JSON.stringify(value);
78+
// JSON.stringify returns undefined (not a string) for functions/symbols/undefined; fall back to
79+
// a tag string so this `string`-typed helper never leaks undefined (matches src/infra/errors.ts).
80+
return JSON.stringify(value) ?? Object.prototype.toString.call(value);
7981
} catch {
8082
return Object.prototype.toString.call(value);
8183
}

0 commit comments

Comments
 (0)