Skip to content

Commit 994b632

Browse files
authored
fix(ext/console): Error Cause Not Inspect-Formatted when printed (#24526)
This pull request addresses an issue where the Error.cause property was not formatted correctly when printed using console.log, leading to confusion. solution: Implemented a fix to ensure that Error.cause is formatted properly when printed by console.log, and the fix done by using JSON.stringify This PR fixes #23416 --------- Signed-off-by: MujahedSafaa <[email protected]>
1 parent 4e8f587 commit 994b632

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ext/console/01_console.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,12 +1484,18 @@ function inspectError(value, ctx) {
14841484
finalMessage += `[${stack || ErrorPrototypeToString(value)}]`;
14851485
}
14861486
}
1487+
const doubleQuoteRegExp = new SafeRegExp('"', "g");
14871488
finalMessage += ArrayPrototypeJoin(
14881489
ArrayPrototypeMap(
14891490
causes,
14901491
(cause) =>
14911492
"\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") +
1492-
(cause?.stack ?? cause),
1493+
(cause?.stack ??
1494+
StringPrototypeReplace(
1495+
inspect(cause),
1496+
doubleQuoteRegExp,
1497+
"",
1498+
)),
14931499
),
14941500
"",
14951501
);

tests/unit/console_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,22 @@ Deno.test(function inspectErrorCircular() {
22062206
);
22072207
});
22082208

2209+
Deno.test(function inspectErrorWithCauseFormat() {
2210+
const error = new Error("This is an error", {
2211+
cause: {
2212+
code: 100500,
2213+
},
2214+
});
2215+
assertStringIncludes(
2216+
stripColor(Deno.inspect(error)),
2217+
"Error: This is an error",
2218+
);
2219+
assertStringIncludes(
2220+
stripColor(Deno.inspect(error)),
2221+
"Caused by { code: 100500 }",
2222+
);
2223+
});
2224+
22092225
Deno.test(function inspectColors() {
22102226
assertEquals(Deno.inspect(1), "1");
22112227
assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b[");

0 commit comments

Comments
 (0)