mirror of
https://github.com/denoland/deno.git
synced 2024-12-01 16:51:13 -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") {
|
if (typeof instance !== "object") {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if (instance && instance.__proto__ && instance.__proto__.constructor) {
|
if (instance) {
|
||||||
return instance.__proto__.constructor.name; // could be "Object" or "Array"
|
const proto = Object.getPrototypeOf(instance);
|
||||||
|
if (proto && proto.constructor) {
|
||||||
|
return proto.constructor.name; // could be "Object" or "Array"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// tslint:disable-next-line:no-any
|
function createFunctionString(value: Function, ctx: ConsoleContext): string {
|
||||||
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":
|
|
||||||
// Might be Function/AsyncFunction/GeneratorFunction
|
// Might be Function/AsyncFunction/GeneratorFunction
|
||||||
const cstrName = value.__proto__.constructor.name;
|
const cstrName = Object.getPrototypeOf(value).constructor.name;
|
||||||
if (value.name && value.name !== "anonymous") {
|
if (value.name && value.name !== "anonymous") {
|
||||||
// from MDN spec
|
// from MDN spec
|
||||||
return `[${cstrName}: ${value.name}]`;
|
return `[${cstrName}: ${value.name}]`;
|
||||||
}
|
}
|
||||||
return `[${cstrName}]`;
|
return `[${cstrName}]`;
|
||||||
case "object":
|
}
|
||||||
if (value === null) {
|
|
||||||
return "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.has(value)) {
|
// tslint:disable-next-line:no-any
|
||||||
return "[Circular]";
|
function createArrayString(value: any[], ctx: ConsoleContext): string {
|
||||||
}
|
|
||||||
|
|
||||||
ctx.add(value);
|
|
||||||
const entries: string[] = [];
|
const entries: string[] = [];
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
for (const el of value) {
|
for (const el of value) {
|
||||||
entries.push(stringifyWithQuotes(ctx, el));
|
entries.push(stringifyWithQuotes(ctx, el));
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.delete(value);
|
ctx.delete(value);
|
||||||
|
|
||||||
if (entries.length === 0) {
|
if (entries.length === 0) {
|
||||||
return "[]";
|
return "[]";
|
||||||
}
|
}
|
||||||
return `[ ${entries.join(", ")} ]`;
|
return `[ ${entries.join(", ")} ]`;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
function createObjectString(value: any, ctx: ConsoleContext): string {
|
||||||
|
const entries: string[] = [];
|
||||||
let baseString = "";
|
let baseString = "";
|
||||||
|
|
||||||
const className = getClassInstanceName(value);
|
const className = getClassInstanceName(value);
|
||||||
|
@ -79,6 +66,37 @@ function stringify(ctx: ConsoleContext, value: any): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
return baseString;
|
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:
|
default:
|
||||||
return "[Not Implemented]";
|
return "[Not Implemented]";
|
||||||
|
|
|
@ -91,3 +91,17 @@ test(function consoleTestStringifyCircular() {
|
||||||
"Console { printFunc: [Function], debug: [Function: log], info: [Function: log], error: [Function: warn] }"
|
"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