diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2021-10-31 02:25:46 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-30 19:25:46 +0200 |
commit | 95b9e5f30f9b5e925b9102430eaee10713cb0aa6 (patch) | |
tree | b2e0b7b30de95e656f687ef0e2cf16ba30a19ab2 | |
parent | 3fb23ab772e87b9314cd8608eb589c72a53efaee (diff) |
feat(ext/console): Display error.cause in console (#12462)
-rw-r--r-- | cli/tests/unit/console_test.ts | 42 | ||||
-rw-r--r-- | ext/console/02_console.js | 22 |
2 files changed, 63 insertions, 1 deletions
diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index b5dcf2f05..4201a2a46 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -1823,6 +1823,48 @@ unitTest(function inspectProxy() { ); }); +unitTest(function inspectError() { + const error1 = new Error("This is an error"); + const error2 = new Error("This is an error", { + cause: new Error("This is a cause error"), + }); + + assertStringIncludes( + stripColor(Deno.inspect(error1)), + "Error: This is an error", + ); + assertStringIncludes( + stripColor(Deno.inspect(error2)), + "Error: This is an error", + ); + assertStringIncludes( + stripColor(Deno.inspect(error2)), + "Caused by Error: This is a cause error", + ); +}); + +unitTest(function inspectErrorCircular() { + const error1 = new Error("This is an error"); + const error2 = new Error("This is an error", { + cause: new Error("This is a cause error"), + }); + error1.cause = error1; + error2.cause.cause = error2; + + assertStringIncludes( + stripColor(Deno.inspect(error1)), + "Error: This is an error", + ); + assertStringIncludes( + stripColor(Deno.inspect(error2)), + "Error: This is an error", + ); + assertStringIncludes( + stripColor(Deno.inspect(error2)), + "Caused by Error: This is a cause error", + ); +}); + unitTest(function inspectColors() { assertEquals(Deno.inspect(1), "1"); assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b["); diff --git a/ext/console/02_console.js b/ext/console/02_console.js index 09d96fc09..8d650d8ec 100644 --- a/ext/console/02_console.js +++ b/ext/console/02_console.js @@ -883,6 +883,26 @@ return red(RegExpPrototypeToString(value)); // RegExps are red } + function inspectError(value) { + const causes = []; + + let err = value; + while ( + err.cause instanceof Error && err.cause !== value && + !ArrayPrototypeIncludes(causes, err.cause) // circular check + ) { + ArrayPrototypePush(causes, err.cause); + err = err.cause; + } + + return `${value.stack}${ + ArrayPrototypeJoin( + ArrayPrototypeMap(causes, (cause) => `\nCaused by ${cause.stack}`), + "", + ) + }`; + } + function inspectStringObject(value, inspectOptions) { const cyan = maybeColor(colors.cyan, inspectOptions); return cyan(`[String: "${StringPrototypeToString(value)}"]`); // wrappers are in cyan @@ -1141,7 +1161,7 @@ return String(value[privateCustomInspect](inspect)); } if (value instanceof Error) { - return String(value.stack); + return inspectError(value); } else if (ArrayIsArray(value)) { return inspectArray(value, level, inspectOptions); } else if (value instanceof Number) { |