fix(ext/console): Error Cause Not Inspect-Formatted when printed
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
The PR seems to be missing a test case that exercises the change and shows why it was made. What happens when the
.stackproperty holds something thatJSON.stringifycannot deal with like a bigint?
The code run JSONstringify only in case the stack value is null or undefined.
Try running this snippet with the changes in this PR. On my end it causes an unexpected exception:
console.log(new Error("foo", { cause: 9007199254740991n }));
Output:
error: Uncaught (in promise) TypeError: Do not know how to serialize a BigInt
console.log(new Error("foo", { cause: 9007199254740991n }));
^
at stringify (<anonymous>)
at ext:deno_console/01_console.js:1496:13
at Array.map (<anonymous>)
at inspectError (ext:deno_console/01_console.js:1490:5)
at formatRaw (ext:deno_console/01_console.js:852:16)
at formatValue (ext:deno_console/01_console.js:530:10)
at inspectArgs (ext:deno_console/01_console.js:3044:17)
at console.log (ext:deno_console/01_console.js:3113:7)
at file:///Users/marvinh/dev/test/deno-cause/main.ts:1:9
Deno should not error like that. To make it work we must ensure that any JS value can be printed, not just a subset.
Try running this snippet with the changes in this PR. On my end it causes an unexpected exception:
console.log(new Error("foo", { cause: 9007199254740991n }));Output:
error: Uncaught (in promise) TypeError: Do not know how to serialize a BigInt console.log(new Error("foo", { cause: 9007199254740991n })); ^ at stringify (<anonymous>) at ext:deno_console/01_console.js:1496:13 at Array.map (<anonymous>) at inspectError (ext:deno_console/01_console.js:1490:5) at formatRaw (ext:deno_console/01_console.js:852:16) at formatValue (ext:deno_console/01_console.js:530:10) at inspectArgs (ext:deno_console/01_console.js:3044:17) at console.log (ext:deno_console/01_console.js:3113:7) at file:///Users/marvinh/dev/test/deno-cause/main.ts:1:9Deno should not error like that. To make it work we must ensure that any JS value can be printed, not just a subset.
Yes, it causes an error also on my side, I will add a fix to it.
@marvinhagemeister I changed my code to use inspect rather than JSONstringify, since it handles all the data types