Skip to content

Commit

Permalink
fix(ext/console): Error Cause Not Inspect-Formatted when printed (den…
Browse files Browse the repository at this point in the history
…oland#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 denoland#23416

---------

Signed-off-by: MujahedSafaa <168719085 [email protected]>
  • Loading branch information
MujahedSafaa authored Jul 22, 2024
1 parent 4e8f587 commit 994b632
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ext/console/01_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -1484,12 1484,18 @@ function inspectError(value, ctx) {
finalMessage = `[${stack || ErrorPrototypeToString(value)}]`;
}
}
const doubleQuoteRegExp = new SafeRegExp('"', "g");
finalMessage = ArrayPrototypeJoin(
ArrayPrototypeMap(
causes,
(cause) =>
"\nCaused by " (MapPrototypeGet(refMap, cause) ?? "")
(cause?.stack ?? cause),
(cause?.stack ??
StringPrototypeReplace(
inspect(cause),
doubleQuoteRegExp,
"",
)),
),
"",
);
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 2206,22 @@ Deno.test(function inspectErrorCircular() {
);
});

Deno.test(function inspectErrorWithCauseFormat() {
const error = new Error("This is an error", {
cause: {
code: 100500,
},
});
assertStringIncludes(
stripColor(Deno.inspect(error)),
"Error: This is an error",
);
assertStringIncludes(
stripColor(Deno.inspect(error)),
"Caused by { code: 100500 }",
);
});

Deno.test(function inspectColors() {
assertEquals(Deno.inspect(1), "1");
assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b[");
Expand Down

0 comments on commit 994b632

Please sign in to comment.