mirror of
https://github.com/denoland/deno.git
synced 2024-11-29 16:30:56 -05:00
Error pretty print (print stack)
This commit is contained in:
parent
32806b1871
commit
1e390e69cd
2 changed files with 79 additions and 47 deletions
|
@ -6,54 +6,41 @@ function getClassInstanceName(instance: any): string {
|
|||
if (typeof instance !== "object") {
|
||||
return "";
|
||||
}
|
||||
if (instance && instance.__proto__ && instance.__proto__.constructor) {
|
||||
return instance.__proto__.constructor.name; // could be "Object" or "Array"
|
||||
if (instance) {
|
||||
const proto = Object.getPrototypeOf(instance);
|
||||
if (proto && proto.constructor) {
|
||||
return proto.constructor.name; // could be "Object" or "Array"
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
function stringify(ctx: ConsoleContext, value: any): string {
|
||||
switch (typeof value) {
|
||||
case "string":
|
||||
return value;
|
||||
case "number":
|
||||
case "boolean":
|
||||
case "undefined":
|
||||
case "symbol":
|
||||
return String(value);
|
||||
case "function":
|
||||
function createFunctionString(value: Function, ctx: ConsoleContext): string {
|
||||
// Might be Function/AsyncFunction/GeneratorFunction
|
||||
const cstrName = value.__proto__.constructor.name;
|
||||
const cstrName = Object.getPrototypeOf(value).constructor.name;
|
||||
if (value.name && value.name !== "anonymous") {
|
||||
// from MDN spec
|
||||
return `[${cstrName}: ${value.name}]`;
|
||||
}
|
||||
return `[${cstrName}]`;
|
||||
case "object":
|
||||
if (value === null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
if (ctx.has(value)) {
|
||||
return "[Circular]";
|
||||
}
|
||||
|
||||
ctx.add(value);
|
||||
// tslint:disable-next-line:no-any
|
||||
function createArrayString(value: any[], ctx: ConsoleContext): string {
|
||||
const entries: string[] = [];
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
for (const el of value) {
|
||||
entries.push(stringifyWithQuotes(ctx, el));
|
||||
}
|
||||
|
||||
ctx.delete(value);
|
||||
|
||||
if (entries.length === 0) {
|
||||
return "[]";
|
||||
}
|
||||
return `[ ${entries.join(", ")} ]`;
|
||||
} else {
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
function createObjectString(value: any, ctx: ConsoleContext): string {
|
||||
const entries: string[] = [];
|
||||
let baseString = "";
|
||||
|
||||
const className = getClassInstanceName(value);
|
||||
|
@ -80,6 +67,37 @@ function stringify(ctx: ConsoleContext, value: any): string {
|
|||
|
||||
return baseString;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
function stringify(ctx: ConsoleContext, value: any): string {
|
||||
switch (typeof value) {
|
||||
case "string":
|
||||
return value;
|
||||
case "number":
|
||||
case "boolean":
|
||||
case "undefined":
|
||||
case "symbol":
|
||||
return String(value);
|
||||
case "function":
|
||||
return createFunctionString(value as Function, ctx);
|
||||
case "object":
|
||||
if (value === null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
if (ctx.has(value)) {
|
||||
return "[Circular]";
|
||||
}
|
||||
ctx.add(value);
|
||||
|
||||
if (value instanceof Error) {
|
||||
return value.stack! || "";
|
||||
} else if (Array.isArray(value)) {
|
||||
// tslint:disable-next-line:no-any
|
||||
return createArrayString(value as any[], ctx);
|
||||
} else {
|
||||
return createObjectString(value, ctx);
|
||||
}
|
||||
default:
|
||||
return "[Not Implemented]";
|
||||
}
|
||||
|
|
|
@ -91,3 +91,17 @@ test(function consoleTestStringifyCircular() {
|
|||
"Console { printFunc: [Function], debug: [Function: log], info: [Function: log], error: [Function: warn] }"
|
||||
);
|
||||
});
|
||||
|
||||
test(function consoleTestError() {
|
||||
class MyError extends Error {
|
||||
constructor(msg: string) {
|
||||
super(msg);
|
||||
this.name = "MyError";
|
||||
}
|
||||
}
|
||||
try {
|
||||
throw new MyError("This is an error");
|
||||
} catch (e) {
|
||||
assertEqual(stringify(e).split("\n")[0], "MyError: This is an error");
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue