diff options
author | MujahedSafaa <168719085+MujahedSafaa@users.noreply.github.com> | 2024-07-22 14:18:49 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-22 04:18:49 -0700 |
commit | 994b6327d3af5721fa4fcf6fbfe856a6f6da79cf (patch) | |
tree | a6e0d57a580ee6c1d1fea9b5421db6c6cb44f5f1 | |
parent | 4e8f5875bc59ddfb84c8b0b26071a547b49823a9 (diff) |
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 https://github.com/denoland/deno/issues/23416
---------
Signed-off-by: MujahedSafaa <168719085+MujahedSafaa@users.noreply.github.com>
-rw-r--r-- | ext/console/01_console.js | 8 | ||||
-rw-r--r-- | tests/unit/console_test.ts | 16 |
2 files changed, 23 insertions, 1 deletions
diff --git a/ext/console/01_console.js b/ext/console/01_console.js index 90f09d4d1..cffa72d8c 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -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, + "", + )), ), "", ); diff --git a/tests/unit/console_test.ts b/tests/unit/console_test.ts index 49960f663..4c9378bd4 100644 --- a/tests/unit/console_test.ts +++ b/tests/unit/console_test.ts @@ -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["); |