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>
This commit is contained in:
MujahedSafaa 2024-07-22 14:18:49 +03:00 committed by GitHub
parent 4e8f5875bc
commit 994b6327d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View File

@ -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,
"",
)),
),
"",
);

View File

@ -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[");