2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2019-07-29 05:11:08 -04:00
|
|
|
// Some of the code here is adapted directly from V8 and licensed under a BSD
|
|
|
|
// style license available here: https://github.com/v8/v8/blob/24886f2d1c565287d33d71e4109a53bf0b54b75c/LICENSE.v8
|
2020-04-11 02:08:11 -04:00
|
|
|
import * as colors from "./colors.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { applySourceMap, Location } from "./ops/errors.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { assert } from "./util.ts";
|
2020-01-16 19:42:58 -05:00
|
|
|
import { exposeForTest } from "./internals.ts";
|
2019-07-29 05:11:08 -04:00
|
|
|
|
|
|
|
function patchCallSite(callSite: CallSite, location: Location): CallSite {
|
|
|
|
return {
|
|
|
|
getThis(): unknown {
|
|
|
|
return callSite.getThis();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
getTypeName(): string | null {
|
2019-07-29 05:11:08 -04:00
|
|
|
return callSite.getTypeName();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
getFunction(): Function | null {
|
2019-07-29 05:11:08 -04:00
|
|
|
return callSite.getFunction();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
getFunctionName(): string | null {
|
2019-07-29 05:11:08 -04:00
|
|
|
return callSite.getFunctionName();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
getMethodName(): string | null {
|
2019-07-29 05:11:08 -04:00
|
|
|
return callSite.getMethodName();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
getFileName(): string | null {
|
|
|
|
return location.fileName;
|
2019-07-29 05:11:08 -04:00
|
|
|
},
|
|
|
|
getLineNumber(): number {
|
2020-04-13 10:54:16 -04:00
|
|
|
return location.lineNumber;
|
2019-07-29 05:11:08 -04:00
|
|
|
},
|
|
|
|
getColumnNumber(): number {
|
2020-04-13 10:54:16 -04:00
|
|
|
return location.columnNumber;
|
2019-07-29 05:11:08 -04:00
|
|
|
},
|
|
|
|
getEvalOrigin(): string | null {
|
|
|
|
return callSite.getEvalOrigin();
|
|
|
|
},
|
2020-04-13 10:54:16 -04:00
|
|
|
isToplevel(): boolean | null {
|
2019-07-29 05:11:08 -04:00
|
|
|
return callSite.isToplevel();
|
|
|
|
},
|
|
|
|
isEval(): boolean {
|
|
|
|
return callSite.isEval();
|
|
|
|
},
|
|
|
|
isNative(): boolean {
|
|
|
|
return callSite.isNative();
|
|
|
|
},
|
|
|
|
isConstructor(): boolean {
|
|
|
|
return callSite.isConstructor();
|
|
|
|
},
|
|
|
|
isAsync(): boolean {
|
|
|
|
return callSite.isAsync();
|
|
|
|
},
|
|
|
|
isPromiseAll(): boolean {
|
|
|
|
return callSite.isPromiseAll();
|
|
|
|
},
|
|
|
|
getPromiseIndex(): number | null {
|
|
|
|
return callSite.getPromiseIndex();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-07-29 05:11:08 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMethodCall(callSite: CallSite): string {
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
const typeName = callSite.getTypeName();
|
|
|
|
const methodName = callSite.getMethodName();
|
|
|
|
const functionName = callSite.getFunctionName();
|
|
|
|
|
|
|
|
if (functionName) {
|
|
|
|
if (typeName) {
|
|
|
|
const startsWithTypeName = functionName.startsWith(typeName);
|
|
|
|
if (!startsWithTypeName) {
|
|
|
|
result += `${typeName}.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += functionName;
|
|
|
|
|
|
|
|
if (methodName) {
|
|
|
|
if (!functionName.endsWith(methodName)) {
|
|
|
|
result += ` [as ${methodName}]`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeName) {
|
|
|
|
result += `${typeName}.`;
|
|
|
|
}
|
|
|
|
if (methodName) {
|
|
|
|
result += methodName;
|
|
|
|
} else {
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
function getFileLocation(callSite: CallSite, internal = false): string {
|
|
|
|
const cyan = internal ? colors.gray : colors.cyan;
|
|
|
|
const yellow = internal ? colors.gray : colors.yellow;
|
|
|
|
const black = internal ? colors.gray : (s: string): string => s;
|
2019-07-29 05:11:08 -04:00
|
|
|
if (callSite.isNative()) {
|
2020-04-11 02:08:11 -04:00
|
|
|
return cyan("native");
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
if (!fileName && callSite.isEval()) {
|
|
|
|
const evalOrigin = callSite.getEvalOrigin();
|
|
|
|
assert(evalOrigin != null);
|
2020-04-11 02:08:11 -04:00
|
|
|
result += cyan(`${evalOrigin}, `);
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fileName) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += cyan(fileName);
|
2019-07-29 05:11:08 -04:00
|
|
|
} else {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += cyan("<anonymous>");
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
if (lineNumber != null) {
|
2020-06-05 11:37:40 -04:00
|
|
|
result += `${black(":")}${yellow(lineNumber.toString())}`;
|
2019-07-29 05:11:08 -04:00
|
|
|
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (columnNumber != null) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += `${black(":")}${yellow(columnNumber.toString())}`;
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
function callSiteToString(callSite: CallSite, internal = false): string {
|
|
|
|
const cyan = internal ? colors.gray : colors.cyan;
|
|
|
|
const black = internal ? colors.gray : (s: string): string => s;
|
2020-04-11 02:08:11 -04:00
|
|
|
|
2019-07-29 05:11:08 -04:00
|
|
|
let result = "";
|
|
|
|
const functionName = callSite.getFunctionName();
|
|
|
|
|
|
|
|
const isTopLevel = callSite.isToplevel();
|
|
|
|
const isAsync = callSite.isAsync();
|
|
|
|
const isPromiseAll = callSite.isPromiseAll();
|
|
|
|
const isConstructor = callSite.isConstructor();
|
|
|
|
const isMethodCall = !(isTopLevel || isConstructor);
|
|
|
|
|
|
|
|
if (isAsync) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.gray("async ");
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
if (isPromiseAll) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.bold(
|
|
|
|
colors.italic(black(`Promise.all (index ${callSite.getPromiseIndex()})`))
|
|
|
|
);
|
2019-07-29 05:11:08 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (isMethodCall) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.bold(colors.italic(black(getMethodCall(callSite))));
|
2019-07-29 05:11:08 -04:00
|
|
|
} else if (isConstructor) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.gray("new ");
|
2019-07-29 05:11:08 -04:00
|
|
|
if (functionName) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.bold(colors.italic(black(functionName)));
|
2019-07-29 05:11:08 -04:00
|
|
|
} else {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += cyan("<anonymous>");
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
} else if (functionName) {
|
2020-04-11 02:08:11 -04:00
|
|
|
result += colors.bold(colors.italic(black(functionName)));
|
2019-07-29 05:11:08 -04:00
|
|
|
} else {
|
2020-07-06 21:45:39 -04:00
|
|
|
result += getFileLocation(callSite, internal);
|
2019-07-29 05:11:08 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
result += ` ${black("(")}${getFileLocation(callSite, internal)}${black(")")}`;
|
2019-07-29 05:11:08 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:26:52 -04:00
|
|
|
interface CallSiteEval {
|
|
|
|
this: unknown;
|
2020-04-13 10:54:16 -04:00
|
|
|
typeName: string | null;
|
|
|
|
function: Function | null;
|
|
|
|
functionName: string | null;
|
|
|
|
methodName: string | null;
|
|
|
|
fileName: string | null;
|
2020-04-10 12:26:52 -04:00
|
|
|
lineNumber: number | null;
|
|
|
|
columnNumber: number | null;
|
|
|
|
evalOrigin: string | null;
|
2020-04-13 10:54:16 -04:00
|
|
|
isToplevel: boolean | null;
|
2020-04-10 12:26:52 -04:00
|
|
|
isEval: boolean;
|
|
|
|
isNative: boolean;
|
|
|
|
isConstructor: boolean;
|
|
|
|
isAsync: boolean;
|
|
|
|
isPromiseAll: boolean;
|
|
|
|
promiseIndex: number | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateCallSite(callSite: CallSite): CallSiteEval {
|
|
|
|
return {
|
|
|
|
this: callSite.getThis(),
|
|
|
|
typeName: callSite.getTypeName(),
|
|
|
|
function: callSite.getFunction(),
|
|
|
|
functionName: callSite.getFunctionName(),
|
|
|
|
methodName: callSite.getMethodName(),
|
|
|
|
fileName: callSite.getFileName(),
|
|
|
|
lineNumber: callSite.getLineNumber(),
|
|
|
|
columnNumber: callSite.getColumnNumber(),
|
|
|
|
evalOrigin: callSite.getEvalOrigin(),
|
|
|
|
isToplevel: callSite.isToplevel(),
|
|
|
|
isEval: callSite.isEval(),
|
|
|
|
isNative: callSite.isNative(),
|
|
|
|
isConstructor: callSite.isConstructor(),
|
|
|
|
isAsync: callSite.isAsync(),
|
|
|
|
isPromiseAll: callSite.isPromiseAll(),
|
|
|
|
promiseIndex: callSite.getPromiseIndex(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:24:44 -04:00
|
|
|
function prepareStackTrace(
|
|
|
|
error: Error & {
|
|
|
|
__callSiteEvals: CallSiteEval[];
|
|
|
|
__formattedFrames: string[];
|
|
|
|
},
|
|
|
|
callSites: CallSite[]
|
|
|
|
): string {
|
2020-05-29 08:02:36 -04:00
|
|
|
const mappedCallSites = callSites.map(
|
|
|
|
(callSite): CallSite => {
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (fileName && lineNumber != null && columnNumber != null) {
|
|
|
|
return patchCallSite(
|
|
|
|
callSite,
|
|
|
|
applySourceMap({
|
|
|
|
fileName,
|
|
|
|
lineNumber,
|
|
|
|
columnNumber,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return callSite;
|
|
|
|
}
|
|
|
|
);
|
2020-04-11 02:08:11 -04:00
|
|
|
Object.defineProperties(error, {
|
2020-05-29 08:02:36 -04:00
|
|
|
__callSiteEvals: { value: [], configurable: true },
|
|
|
|
__formattedFrames: { value: [], configurable: true },
|
2020-04-11 02:08:11 -04:00
|
|
|
});
|
2020-05-29 08:02:36 -04:00
|
|
|
for (const callSite of mappedCallSites) {
|
|
|
|
error.__callSiteEvals.push(Object.freeze(evaluateCallSite(callSite)));
|
|
|
|
const isInternal = callSite.getFileName()?.startsWith("$deno$") ?? false;
|
|
|
|
error.__formattedFrames.push(callSiteToString(callSite, isInternal));
|
|
|
|
}
|
2020-04-11 02:08:11 -04:00
|
|
|
Object.freeze(error.__callSiteEvals);
|
|
|
|
Object.freeze(error.__formattedFrames);
|
2020-05-29 08:02:36 -04:00
|
|
|
return (
|
|
|
|
`${error.name}: ${error.message}\n` +
|
|
|
|
error.__formattedFrames
|
|
|
|
.map((s: string) => ` at ${colors.stripColor(s)}`)
|
|
|
|
.join("\n")
|
|
|
|
);
|
2019-07-29 05:11:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function setPrepareStackTrace(ErrorConstructor: typeof Error): void {
|
|
|
|
ErrorConstructor.prepareStackTrace = prepareStackTrace;
|
|
|
|
}
|
2020-01-16 19:42:58 -05:00
|
|
|
|
|
|
|
exposeForTest("setPrepareStackTrace", setPrepareStackTrace);
|