Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Log customInspect implementation errors #9095

Merged
merged 5 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions cli/tests/unit/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
assert,
assertEquals,
assertStringIncludes,
assertThrows,
unitTest,
} from "./test_util.ts";
import { stripColor } from "../../../std/fmt/colors.ts";
Expand Down Expand Up @@ -834,19 +835,11 @@ unitTest(function consoleTestWithCustomInspectorError(): void {
}
}

assertEquals(stringify(new A()), "A {}");

class B {
constructor(public field: { a: string }) {}
[customInspect](): string {
return this.field.a;
}
}

assertEquals(stringify(new B({ a: "a" })), "a");
assertEquals(
stringify(B.prototype),
"B { [Symbol(Deno.customInspect)]: [Function: [Deno.customInspect]] }",
assertThrows(
() => stringify(new A()),
Error,
"BOOM",
"Custom inspect won't attempt to parse if user defined function throws",
);
});

Expand Down
18 changes: 3 additions & 15 deletions runtime/js/02_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@

function inspectFunction(value, _ctx) {
if (customInspect in value && typeof value[customInspect] === "function") {
try {
return String(value[customInspect]());
} catch {
// pass
}
return String(value[customInspect]());
}
// Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction
let cstrName = Object.getPrototypeOf(value)?.constructor?.name;
Expand Down Expand Up @@ -865,11 +861,7 @@
inspectOptions,
) {
if (customInspect in value && typeof value[customInspect] === "function") {
try {
return String(value[customInspect]());
} catch {
// pass
}
return String(value[customInspect]());
}
// This non-unique symbol is used to support op_crates, ie.
// in op_crates/web we don't want to depend on unique "Deno.customInspect"
Expand All @@ -880,11 +872,7 @@
nonUniqueCustomInspect in value &&
typeof value[nonUniqueCustomInspect] === "function"
) {
try {
return String(value[nonUniqueCustomInspect]());
} catch {
// pass
}
return String(value[nonUniqueCustomInspect]());
}
if (value instanceof Error) {
return String(value.stack);
Expand Down