From da798b74475558bcccb3583c2ca50aafcc664328 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Thu, 11 Jul 2024 13:01:49 +0300 Subject: [PATCH 1/8] fix(ext/console): Ensure Error.cause is properly formatted when using console.log --- ext/console/01_console.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index 90f09d4d1741c6..85e7c5fcbb1cba 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -1489,7 +1489,7 @@ function inspectError(value, ctx) { causes, (cause) => "\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") + - (cause?.stack ?? cause), + (cause?.stack ?? JSON.stringify(cause, null, 2)), ), "", ); From 5d12a8b89023e2dd9058d3221184dbffa234f9e8 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Thu, 11 Jul 2024 15:55:23 +0300 Subject: [PATCH 2/8] Use the equivalent for "JSON.stringify" from the `primordials` object --- ext/console/01_console.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index 85e7c5fcbb1cba..90b277ae3bca85 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -66,6 +66,7 @@ const { FunctionPrototypeBind, FunctionPrototypeCall, FunctionPrototypeToString, + JSONStringify, MapPrototypeDelete, MapPrototypeEntries, MapPrototypeForEach, @@ -1489,7 +1490,7 @@ function inspectError(value, ctx) { causes, (cause) => "\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") + - (cause?.stack ?? JSON.stringify(cause, null, 2)), + (cause?.stack ?? JSONStringify(cause, null, 2)), ), "", ); From 9e728dc1104f9600ccb1517e21583182fd85ddba Mon Sep 17 00:00:00 2001 From: Mujahed Date: Mon, 15 Jul 2024 12:22:36 +0300 Subject: [PATCH 3/8] Fix failed test inspectErrorCircular --- ext/console/01_console.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index 90b277ae3bca85..e9eeabf4e04434 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -1485,12 +1485,13 @@ function inspectError(value, ctx) { finalMessage += `[${stack || ErrorPrototypeToString(value)}]`; } } + console.log("print 3: " + causes); finalMessage += ArrayPrototypeJoin( ArrayPrototypeMap( causes, (cause) => "\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") + - (cause?.stack ?? JSONStringify(cause, null, 2)), + (cause?.stack ?? JSONStringify(cause, null, 2).replace(/"/g, '')), ), "", ); From 41d9b4d9f8e566bb5fcdb7eb13d975ad4d3a1443 Mon Sep 17 00:00:00 2001 From: MujahedSafaa <168719085+MujahedSafaa@users.noreply.github.com> Date: Mon, 15 Jul 2024 12:24:28 +0300 Subject: [PATCH 4/8] Update 01_console.js Signed-off-by: MujahedSafaa <168719085+MujahedSafaa@users.noreply.github.com> --- ext/console/01_console.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index e9eeabf4e04434..e2499aa447d80a 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -1485,7 +1485,7 @@ function inspectError(value, ctx) { finalMessage += `[${stack || ErrorPrototypeToString(value)}]`; } } - console.log("print 3: " + causes); + finalMessage += ArrayPrototypeJoin( ArrayPrototypeMap( causes, From 30234d967532c7b8cb0cb34d7f658d79b02a0157 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Mon, 15 Jul 2024 12:37:32 +0300 Subject: [PATCH 5/8] format code based on lint rules --- ext/console/01_console.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index e2499aa447d80a..f79e89705e91a2 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -1485,13 +1485,12 @@ function inspectError(value, ctx) { finalMessage += `[${stack || ErrorPrototypeToString(value)}]`; } } - finalMessage += ArrayPrototypeJoin( ArrayPrototypeMap( causes, (cause) => "\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") + - (cause?.stack ?? JSONStringify(cause, null, 2).replace(/"/g, '')), + (cause?.stack ?? JSONStringify(cause, null, 2).replace(/"/g, "")), ), "", ); From 901810f534b8841a16bbf57471e4275b93183354 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Mon, 15 Jul 2024 14:53:54 +0300 Subject: [PATCH 6/8] Enhance code readability --- ext/console/01_console.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index f79e89705e91a2..7c3d6a1f30894e 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -1485,12 +1485,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 ?? JSONStringify(cause, null, 2).replace(/"/g, "")), + (cause?.stack ?? + StringPrototypeReplace( + JSONStringify(cause, null, 2), + doubleQuoteRegExp, + "", + )), ), "", ); From 5e91199985506eb838885916e27245d5b96a9c71 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Tue, 16 Jul 2024 15:23:16 +0300 Subject: [PATCH 7/8] Add test use case --- tests/unit/console_test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/unit/console_test.ts b/tests/unit/console_test.ts index 49960f66393efb..ea365c4d8e5388 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 {\n code: 100500\n}", + ); +}); + Deno.test(function inspectColors() { assertEquals(Deno.inspect(1), "1"); assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b["); From 8817772e6f5ed349dd96b34388b88fe2c7864d91 Mon Sep 17 00:00:00 2001 From: Mujahed Date: Wed, 17 Jul 2024 12:19:56 +0300 Subject: [PATCH 8/8] Change from stringify to inspect to handle all types of data --- ext/console/01_console.js | 3 +-- tests/unit/console_test.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/console/01_console.js b/ext/console/01_console.js index 7c3d6a1f30894e..cffa72d8cdc1ae 100644 --- a/ext/console/01_console.js +++ b/ext/console/01_console.js @@ -66,7 +66,6 @@ const { FunctionPrototypeBind, FunctionPrototypeCall, FunctionPrototypeToString, - JSONStringify, MapPrototypeDelete, MapPrototypeEntries, MapPrototypeForEach, @@ -1493,7 +1492,7 @@ function inspectError(value, ctx) { "\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") + (cause?.stack ?? StringPrototypeReplace( - JSONStringify(cause, null, 2), + inspect(cause), doubleQuoteRegExp, "", )), diff --git a/tests/unit/console_test.ts b/tests/unit/console_test.ts index ea365c4d8e5388..4c9378bd45c7f2 100644 --- a/tests/unit/console_test.ts +++ b/tests/unit/console_test.ts @@ -2218,7 +2218,7 @@ Deno.test(function inspectErrorWithCauseFormat() { ); assertStringIncludes( stripColor(Deno.inspect(error)), - "Caused by {\n code: 100500\n}", + "Caused by { code: 100500 }", ); });