2020-07-22 12:03:46 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
// 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
|
|
|
|
const assert = window.__bootstrap.util.assert;
|
|
|
|
|
|
|
|
function patchCallSite(callSite, location) {
|
|
|
|
return {
|
|
|
|
getThis() {
|
|
|
|
return callSite.getThis();
|
|
|
|
},
|
|
|
|
getTypeName() {
|
|
|
|
return callSite.getTypeName();
|
|
|
|
},
|
|
|
|
getFunction() {
|
|
|
|
return callSite.getFunction();
|
|
|
|
},
|
|
|
|
getFunctionName() {
|
|
|
|
return callSite.getFunctionName();
|
|
|
|
},
|
|
|
|
getMethodName() {
|
|
|
|
return callSite.getMethodName();
|
|
|
|
},
|
|
|
|
getFileName() {
|
|
|
|
return location.fileName;
|
|
|
|
},
|
|
|
|
getLineNumber() {
|
|
|
|
return location.lineNumber;
|
|
|
|
},
|
|
|
|
getColumnNumber() {
|
|
|
|
return location.columnNumber;
|
|
|
|
},
|
|
|
|
getEvalOrigin() {
|
|
|
|
return callSite.getEvalOrigin();
|
|
|
|
},
|
|
|
|
isToplevel() {
|
|
|
|
return callSite.isToplevel();
|
|
|
|
},
|
|
|
|
isEval() {
|
|
|
|
return callSite.isEval();
|
|
|
|
},
|
|
|
|
isNative() {
|
|
|
|
return callSite.isNative();
|
|
|
|
},
|
|
|
|
isConstructor() {
|
|
|
|
return callSite.isConstructor();
|
|
|
|
},
|
|
|
|
isAsync() {
|
|
|
|
return callSite.isAsync();
|
|
|
|
},
|
|
|
|
isPromiseAll() {
|
|
|
|
return callSite.isPromiseAll();
|
|
|
|
},
|
|
|
|
getPromiseIndex() {
|
|
|
|
return callSite.getPromiseIndex();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:01:30 -04:00
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
|
|
|
function formatLocation(callSite) {
|
2020-07-22 12:03:46 -04:00
|
|
|
if (callSite.isNative()) {
|
2020-07-23 09:29:36 -04:00
|
|
|
return "native";
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
|
|
|
|
if (fileName) {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += fileName;
|
2020-07-22 12:03:46 -04:00
|
|
|
} else {
|
2020-09-22 13:01:30 -04:00
|
|
|
if (callSite.isEval()) {
|
|
|
|
const evalOrigin = callSite.getEvalOrigin();
|
|
|
|
assert(evalOrigin != null);
|
|
|
|
result += `${evalOrigin}, `;
|
|
|
|
}
|
2020-07-23 09:29:36 -04:00
|
|
|
result += "<anonymous>";
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
if (lineNumber != null) {
|
2020-09-22 13:01:30 -04:00
|
|
|
result += `:${lineNumber}`;
|
2020-07-22 12:03:46 -04:00
|
|
|
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (columnNumber != null) {
|
2020-09-22 13:01:30 -04:00
|
|
|
result += `:${columnNumber}`;
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:01:30 -04:00
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
|
|
|
function formatCallSite(callSite) {
|
2020-07-22 12:03:46 -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-07-23 09:29:36 -04:00
|
|
|
result += "async ";
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
if (isPromiseAll) {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += `Promise.all (index ${callSite.getPromiseIndex()})`;
|
2020-07-22 12:03:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (isMethodCall) {
|
2020-09-22 13:01:30 -04:00
|
|
|
const typeName = callSite.getTypeName();
|
|
|
|
const methodName = callSite.getMethodName();
|
|
|
|
|
|
|
|
if (functionName) {
|
|
|
|
if (typeName) {
|
|
|
|
if (!functionName.startsWith(typeName)) {
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 12:03:46 -04:00
|
|
|
} else if (isConstructor) {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += "new ";
|
2020-07-22 12:03:46 -04:00
|
|
|
if (functionName) {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += functionName;
|
2020-07-22 12:03:46 -04:00
|
|
|
} else {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += "<anonymous>";
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
} else if (functionName) {
|
2020-07-23 09:29:36 -04:00
|
|
|
result += functionName;
|
2020-07-22 12:03:46 -04:00
|
|
|
} else {
|
2020-09-22 13:01:30 -04:00
|
|
|
result += formatLocation(callSite);
|
2020-07-22 12:03:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:01:30 -04:00
|
|
|
result += ` (${formatLocation(callSite)})`;
|
2020-07-22 12:03:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateCallSite(callSite) {
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareStackTrace(
|
|
|
|
error,
|
|
|
|
callSites,
|
|
|
|
) {
|
|
|
|
const mappedCallSites = callSites.map(
|
|
|
|
(callSite) => {
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (fileName && lineNumber != null && columnNumber != null) {
|
|
|
|
return patchCallSite(
|
|
|
|
callSite,
|
2020-07-23 09:29:36 -04:00
|
|
|
{
|
2020-07-22 12:03:46 -04:00
|
|
|
fileName,
|
|
|
|
lineNumber,
|
|
|
|
columnNumber,
|
2020-07-23 09:29:36 -04:00
|
|
|
},
|
2020-07-22 12:03:46 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return callSite;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Object.defineProperties(error, {
|
|
|
|
__callSiteEvals: { value: [], configurable: true },
|
|
|
|
});
|
2020-09-22 13:01:30 -04:00
|
|
|
const formattedCallSites = [];
|
2020-07-22 12:03:46 -04:00
|
|
|
for (const callSite of mappedCallSites) {
|
|
|
|
error.__callSiteEvals.push(Object.freeze(evaluateCallSite(callSite)));
|
2020-09-22 13:01:30 -04:00
|
|
|
formattedCallSites.push(formatCallSite(callSite));
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
Object.freeze(error.__callSiteEvals);
|
|
|
|
return (
|
|
|
|
`${error.name}: ${error.message}\n` +
|
2020-09-22 13:01:30 -04:00
|
|
|
formattedCallSites
|
|
|
|
.map((s) => ` at ${s}`)
|
2020-07-22 12:03:46 -04:00
|
|
|
.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPrepareStackTrace(ErrorConstructor) {
|
|
|
|
ErrorConstructor.prepareStackTrace = prepareStackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.errorStack = {
|
|
|
|
setPrepareStackTrace,
|
|
|
|
};
|
|
|
|
})(this);
|