fix: don't swallow customInspect exceptions (#9095)

This commit is contained in:
Steven Guerrero 2021-01-14 14:18:51 -05:00 committed by GitHub
parent 979d71c883
commit 2d1208556a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 28 deletions

View file

@ -12,6 +12,7 @@ import {
assert, assert,
assertEquals, assertEquals,
assertStringIncludes, assertStringIncludes,
assertThrows,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
import { stripColor } from "../../../std/fmt/colors.ts"; import { stripColor } from "../../../std/fmt/colors.ts";
@ -834,19 +835,11 @@ unitTest(function consoleTestWithCustomInspectorError(): void {
} }
} }
assertEquals(stringify(new A()), "A {}"); assertThrows(
() => stringify(new A()),
class B { Error,
constructor(public field: { a: string }) {} "BOOM",
[customInspect](): string { "Custom inspect won't attempt to parse if user defined function throws",
return this.field.a;
}
}
assertEquals(stringify(new B({ a: "a" })), "a");
assertEquals(
stringify(B.prototype),
"B { [Symbol(Deno.customInspect)]: [Function: [Deno.customInspect]] }",
); );
}); });

View file

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