1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

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 Bartek Iwańczuk
parent 3c40457820
commit 94a5eaf5b8
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
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[");