fix console.log when error has been caught (#2041)

This commit is contained in:
迷渡 2019-04-03 20:38:50 +08:00 committed by Ryan Dahl
parent 6463a75b44
commit 5f97c041d9
2 changed files with 20 additions and 5 deletions

View file

@ -4,8 +4,6 @@ import { TypedArray } from "./types";
import { TextEncoder } from "./text_encoding";
import { File, stdout } from "./files";
import { cliTable } from "./console_table";
import { formatError } from "./format_error";
import { core } from "./core";
type ConsoleContext = Set<unknown>;
type ConsoleOptions = Partial<{
@ -323,8 +321,7 @@ function createObjectString(
...args: [ConsoleContext, number, number]
): string {
if (value instanceof Error) {
const errorJSON = core.errorToJSON(value);
return formatError(errorJSON);
return String(value.stack);
} else if (Array.isArray(value)) {
return createArrayString(value, ...args);
} else if (value instanceof Number) {

View file

@ -264,7 +264,7 @@ test(function consoleTestError() {
} catch (e) {
assert(
stringify(e)
.split("\n")[3]
.split("\n")[0] // error has been caught
.includes("MyError: This is an error")
);
}
@ -593,3 +593,21 @@ test(function consoleTable() {
assertEquals(out.toString(), "test\n");
});
});
// console.log(Error) test
test(function consoleLogShouldNotThrowError() {
let result = 0;
try {
console.log(new Error("foo"));
result = 1;
} catch (e) {
result = 2;
}
assertEquals(result, 1);
// output errors to the console should not include "Uncaught"
mockConsole((console, out) => {
console.log(new Error("foo"));
assertEquals(out.toString().includes("Uncaught"), false);
});
});