Skip to content

Commit 5db8368

Browse files
authored
fix(errors): drop cause text the message already states (#125687)
* fix(errors): drop cause text the message already states Cause-chain dedupe compared whole strings, so a wrapper that embeds its cause verbatim printed it twice, and an errno detail was followed by its own bare code. Skip any cause segment already contained in the accumulated message. * test(backup): stop pinning the duplicated errno suffix Both debug-view assertions required the bare code to follow the errno detail that already names it. Assert the detail itself instead, so they pin the message content rather than the redundancy. * fix(errors): narrow cause dedupe to wrapper-embedded messages Suppressing every contained segment also dropped trailing bare codes, which cron, fs, and backup tests pin deliberately as this formatter's convention. Restrict containment to cause messages so the wrapper duplication is fixed without changing the code suffix, and restore the backup assertions.
1 parent 9ad1c0c commit 5db8368

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ describe("formatErrorMessage", () => {
2626
);
2727
});
2828

29+
it("omits cause text the wrapper message already spells out", () => {
30+
// Wrappers that embed the cause verbatim printed the whole sentence twice.
31+
const parseFailure = new SyntaxError("JSON5: invalid character 'j' at 1:7");
32+
const wrapped = new Error(`Failed to parse --file as JSON5: ${parseFailure.message}`, {
33+
cause: parseFailure,
34+
});
35+
expect(format(wrapped)).toBe(
36+
"Failed to parse --file as JSON5: JSON5: invalid character 'j' at 1:7",
37+
);
38+
39+
// Codes keep their own segment even when the detail already names them.
40+
const errno = Object.assign(
41+
new Error("ENOENT: no such file or directory, open '/tmp/missing.json'"),
42+
{ code: "ENOENT" },
43+
);
44+
const notFound = new Error("--file not found: /tmp/missing.json.", { cause: errno });
45+
expect(format(notFound)).toBe(
46+
"--file not found: /tmp/missing.json. | ENOENT: no such file or directory, open '/tmp/missing.json' | ENOENT",
47+
);
48+
});
49+
2950
it("formats status/code records and structured non-Error causes", () => {
3051
expect(format({ status: 500, code: "EPIPE" })).toBe("status=500 code=EPIPE");
3152
expect(format({ status: 404 })).toBe("status=404 code=unknown");

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ export function formatErrorMessage(value: unknown, options: FormatErrorMessageOp
8787
formatted += ` | ${message}`;
8888
seenMessages.add(message);
8989
};
90+
// Wrappers routinely embed the cause verbatim ("failed to parse X: <cause.message>"),
91+
// which exact-match dedupe misses, so the whole sentence prints twice. Codes stay on
92+
// their own: a trailing bare code is this formatter's convention even when the detail
93+
// already names it.
94+
const appendCauseErrorMessage = (message: string | undefined): void => {
95+
if (message && formatted.includes(message)) {
96+
seenMessages.add(message);
97+
return;
98+
}
99+
appendCauseMessage(message);
100+
};
90101
if (options.includeCode) {
91102
const code = readProperty(value, "code");
92103
if (typeof code === "string" || typeof code === "number") {
@@ -96,7 +107,7 @@ export function formatErrorMessage(value: unknown, options: FormatErrorMessageOp
96107
while (cause && !seen.has(cause)) {
97108
seen.add(cause);
98109
if (cause instanceof Error) {
99-
appendCauseMessage(cause.message);
110+
appendCauseErrorMessage(cause.message);
100111
const code = readProperty(cause, "code");
101112
if (typeof code === "string" || typeof code === "number") {
102113
appendCauseMessage(String(code));

0 commit comments

Comments
 (0)