提交 2f0f236d 编写于 作者: K Kevin (Kun) "Kassimo" Qian 提交者: Ry Dahl

Prevent customInspect error from crashing console (#3226)

上级 f4847763
......@@ -327,8 +327,11 @@ function createObjectString(
...args: [ConsoleContext, number, number]
): string {
if (customInspect in value && typeof value[customInspect] === "function") {
return String(value[customInspect]!());
} else if (value instanceof Error) {
try {
return String(value[customInspect]!());
} catch {}
}
if (value instanceof Error) {
return String(value.stack);
} else if (Array.isArray(value)) {
return createArrayString(value, ...args);
......
......@@ -190,6 +190,27 @@ test(function consoleTestWithCustomInspector(): void {
assertEquals(stringify(new A()), "b");
});
test(function consoleTestWithCustomInspectorError(): void {
class A {
[customInspect](): string {
throw new Error("BOOM");
return "b";
}
}
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), "{}");
});
test(function consoleTestWithIntegerFormatSpecifier(): void {
assertEquals(stringify("%i"), "%i");
assertEquals(stringify("%i", 42.0), "42");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册