Skip to content

Commit 587c752

Browse files
ruel225claudealtaywtf
authored
fix(normalization-core): preserve non-Error object causes with extra keys in formatErrorMessage (#126654)
* fix(normalization-core): preserve non-Error object causes with extra keys The cause-chain branch of formatErrorMessage called only formatStatusAndCode(cause) with no stringifyUnknown fallback, while the top-level branch used formatStatusAndCode(value) ?? stringifyUnknown(value). formatStatusAndCode returns undefined for any object whose keys are not exactly status/code, so a non-Error object cause carrying extra keys (e.g. { statusCode: 429 } or { status: 503, code: "UNAVAILABLE", requestId: "abc" }) was silently dropped — appendCauseMessage(undefined) no-op'd and the loop broke, losing the diagnostic/retryable detail. Mirror the top-level branch: appendCauseMessage(formatStatusAndCode(cause) ?? stringifyUnknown(cause)). Behavior-neutral for causes that already render; restores the dropped detail for the asymmetric case. stringifyUnknown is a local helper in the same file. Closes #126652 Co-Authored-By: Claude <[email protected]> * test(normalization-core): assert structured cause metadata --------- Co-authored-by: ruel225 <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Altay <[email protected]>
1 parent efbcb57 commit 587c752

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,21 @@ describe("formatErrorMessage", () => {
5555
expect(format(new Error("request failed", { cause: { status: 429 } }))).toBe(
5656
"request failed | status=429 code=unknown",
5757
);
58+
// A non-Error cause carrying recognized status/code fields alongside extra
59+
// keys used to be dropped entirely: formatStatusAndCode returns undefined
60+
// for any object with keys beyond status/code, and the cause-chain branch
61+
// had no stringifyUnknown fallback (unlike the top-level branch). The
62+
// structured detail now survives instead of being swallowed.
5863
expect(format(new Error("request failed", { cause: { statusCode: 429 } }))).toBe(
59-
"request failed",
64+
'request failed | {"statusCode":429}',
6065
);
66+
expect(
67+
format(
68+
new Error("request failed", {
69+
cause: { status: 503, code: "UNAVAILABLE", requestId: "abc" },
70+
}),
71+
),
72+
).toBe('request failed | {"status":503,"code":"UNAVAILABLE","requestId":"abc"}');
6173
});
6274

6375
it("stringifies primitives and circular records without throwing", () => {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ export function formatErrorMessage(value: unknown, options: FormatErrorMessageOp
117117
appendCauseMessage(cause);
118118
break;
119119
} else {
120-
appendCauseMessage(formatStatusAndCode(cause));
120+
// Mirror the top-level branch: an object cause with keys beyond
121+
// status/code makes formatStatusAndCode return undefined, so fall
122+
// back to stringifyUnknown rather than dropping the cause entirely.
123+
appendCauseMessage(formatStatusAndCode(cause) ?? stringifyUnknown(cause));
121124
break;
122125
}
123126
}

0 commit comments

Comments
 (0)