mirror of
https://github.com/denoland/deno.git
synced 2024-12-11 18:17:48 -05:00
refactor(core/error): use evaluated call sites for formatting (#15369)
This commit is contained in:
parent
b50088c247
commit
5ceaecbae4
2 changed files with 49 additions and 136 deletions
|
@ -17,6 +17,7 @@
|
||||||
Promise,
|
Promise,
|
||||||
ObjectFromEntries,
|
ObjectFromEntries,
|
||||||
MapPrototypeGet,
|
MapPrototypeGet,
|
||||||
|
MapPrototypeHas,
|
||||||
MapPrototypeDelete,
|
MapPrototypeDelete,
|
||||||
MapPrototypeSet,
|
MapPrototypeSet,
|
||||||
PromisePrototypeThen,
|
PromisePrototypeThen,
|
||||||
|
|
184
core/02_error.js
184
core/02_error.js
|
@ -15,156 +15,81 @@
|
||||||
ArrayPrototypeJoin,
|
ArrayPrototypeJoin,
|
||||||
} = window.__bootstrap.primordials;
|
} = window.__bootstrap.primordials;
|
||||||
|
|
||||||
// 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
|
|
||||||
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();
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep in sync with `cli/fmt_errors.rs`.
|
// Keep in sync with `cli/fmt_errors.rs`.
|
||||||
function formatLocation(callSite) {
|
function formatLocation(cse) {
|
||||||
if (callSite.isNative()) {
|
if (cse.isNative) {
|
||||||
return "native";
|
return "native";
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = "";
|
let result = "";
|
||||||
|
if (cse.fileName) {
|
||||||
const fileName = callSite.getFileName();
|
result += core.opSync("op_format_file_name", cse.fileName);
|
||||||
|
|
||||||
if (fileName) {
|
|
||||||
result += core.opSync("op_format_file_name", fileName);
|
|
||||||
} else {
|
} else {
|
||||||
if (callSite.isEval()) {
|
if (cse.isEval) {
|
||||||
const evalOrigin = callSite.getEvalOrigin();
|
if (cse.evalOrigin == null) {
|
||||||
if (evalOrigin == null) {
|
|
||||||
throw new Error("assert evalOrigin");
|
throw new Error("assert evalOrigin");
|
||||||
}
|
}
|
||||||
result += `${evalOrigin}, `;
|
result += `${cse.evalOrigin}, `;
|
||||||
}
|
}
|
||||||
result += "<anonymous>";
|
result += "<anonymous>";
|
||||||
}
|
}
|
||||||
|
if (cse.lineNumber != null) {
|
||||||
const lineNumber = callSite.getLineNumber();
|
result += `:${cse.lineNumber}`;
|
||||||
if (lineNumber != null) {
|
if (cse.columnNumber != null) {
|
||||||
result += `:${lineNumber}`;
|
result += `:${cse.columnNumber}`;
|
||||||
|
|
||||||
const columnNumber = callSite.getColumnNumber();
|
|
||||||
if (columnNumber != null) {
|
|
||||||
result += `:${columnNumber}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep in sync with `cli/fmt_errors.rs`.
|
// Keep in sync with `cli/fmt_errors.rs`.
|
||||||
function formatCallSite(callSite) {
|
function formatCallSiteEval(cse) {
|
||||||
let result = "";
|
let result = "";
|
||||||
const functionName = callSite.getFunctionName();
|
if (cse.isAsync) {
|
||||||
|
|
||||||
const isTopLevel = callSite.isToplevel();
|
|
||||||
const isAsync = callSite.isAsync();
|
|
||||||
const isPromiseAll = callSite.isPromiseAll();
|
|
||||||
const isConstructor = callSite.isConstructor();
|
|
||||||
const isMethodCall = !(isTopLevel || isConstructor);
|
|
||||||
|
|
||||||
if (isAsync) {
|
|
||||||
result += "async ";
|
result += "async ";
|
||||||
}
|
}
|
||||||
if (isPromiseAll) {
|
if (cse.isPromiseAll) {
|
||||||
result += `Promise.all (index ${callSite.getPromiseIndex()})`;
|
result += `Promise.all (index ${cse.promiseIndex})`;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
const isMethodCall = !(cse.isToplevel || cse.isConstructor);
|
||||||
if (isMethodCall) {
|
if (isMethodCall) {
|
||||||
const typeName = callSite.getTypeName();
|
if (cse.functionName) {
|
||||||
const methodName = callSite.getMethodName();
|
if (cse.typeName) {
|
||||||
|
if (!StringPrototypeStartsWith(cse.functionName, cse.typeName)) {
|
||||||
if (functionName) {
|
result += `${cse.typeName}.`;
|
||||||
if (typeName) {
|
|
||||||
if (!StringPrototypeStartsWith(functionName, typeName)) {
|
|
||||||
result += `${typeName}.`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result += functionName;
|
result += cse.functionName;
|
||||||
if (methodName) {
|
if (cse.methodName) {
|
||||||
if (!StringPrototypeEndsWith(functionName, methodName)) {
|
if (!StringPrototypeEndsWith(cse.functionName, cse.methodName)) {
|
||||||
result += ` [as ${methodName}]`;
|
result += ` [as ${cse.methodName}]`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (typeName) {
|
if (cse.typeName) {
|
||||||
result += `${typeName}.`;
|
result += `${cse.typeName}.`;
|
||||||
}
|
}
|
||||||
if (methodName) {
|
if (cse.methodName) {
|
||||||
result += methodName;
|
result += cse.methodName;
|
||||||
} else {
|
} else {
|
||||||
result += "<anonymous>";
|
result += "<anonymous>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isConstructor) {
|
} else if (cse.isConstructor) {
|
||||||
result += "new ";
|
result += "new ";
|
||||||
if (functionName) {
|
if (cse.functionName) {
|
||||||
result += functionName;
|
result += cse.functionName;
|
||||||
} else {
|
} else {
|
||||||
result += "<anonymous>";
|
result += "<anonymous>";
|
||||||
}
|
}
|
||||||
} else if (functionName) {
|
} else if (cse.functionName) {
|
||||||
result += functionName;
|
result += cse.functionName;
|
||||||
} else {
|
} else {
|
||||||
result += formatLocation(callSite);
|
result += formatLocation(cse);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result += ` (${formatLocation(callSite)})`;
|
result += ` (${formatLocation(cse)})`;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,37 +114,24 @@
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sourceMapCallSiteEval(cse) {
|
||||||
|
if (cse.fileName && cse.lineNumber != null && cse.columnNumber != null) {
|
||||||
|
return { ...cse, ...core.opSync("op_apply_source_map", cse) };
|
||||||
|
}
|
||||||
|
return cse;
|
||||||
|
}
|
||||||
|
|
||||||
/** A function that can be used as `Error.prepareStackTrace`. */
|
/** A function that can be used as `Error.prepareStackTrace`. */
|
||||||
function prepareStackTrace(
|
function prepareStackTrace(error, callSites) {
|
||||||
error,
|
let callSiteEvals = ArrayPrototypeMap(callSites, evaluateCallSite);
|
||||||
callSites,
|
callSiteEvals = ArrayPrototypeMap(callSiteEvals, sourceMapCallSiteEval);
|
||||||
) {
|
|
||||||
const mappedCallSites = ArrayPrototypeMap(callSites, (callSite) => {
|
|
||||||
const fileName = callSite.getFileName();
|
|
||||||
const lineNumber = callSite.getLineNumber();
|
|
||||||
const columnNumber = callSite.getColumnNumber();
|
|
||||||
if (fileName && lineNumber != null && columnNumber != null) {
|
|
||||||
return patchCallSite(
|
|
||||||
callSite,
|
|
||||||
core.opSync("op_apply_source_map", {
|
|
||||||
fileName,
|
|
||||||
lineNumber,
|
|
||||||
columnNumber,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return callSite;
|
|
||||||
});
|
|
||||||
ObjectDefineProperties(error, {
|
ObjectDefineProperties(error, {
|
||||||
__callSiteEvals: { value: [], configurable: true },
|
__callSiteEvals: { value: [], configurable: true },
|
||||||
});
|
});
|
||||||
const formattedCallSites = [];
|
const formattedCallSites = [];
|
||||||
for (const callSite of mappedCallSites) {
|
for (const cse of callSiteEvals) {
|
||||||
ArrayPrototypePush(error.__callSiteEvals, evaluateCallSite(callSite));
|
ArrayPrototypePush(error.__callSiteEvals, cse);
|
||||||
ArrayPrototypePush(
|
ArrayPrototypePush(formattedCallSites, formatCallSiteEval(cse));
|
||||||
formattedCallSites,
|
|
||||||
formatCallSite(callSite),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
const message = error.message !== undefined ? error.message : "";
|
const message = error.message !== undefined ? error.message : "";
|
||||||
const name = error.name !== undefined ? error.name : "Error";
|
const name = error.name !== undefined ? error.name : "Error";
|
||||||
|
|
Loading…
Reference in a new issue