From 8b4508338b15dcc08503553dc9179a154c0165c7 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 10 Apr 2020 17:26:52 +0100 Subject: fix(core/js_error): Get frame data from prepareStackTrace() (#4690) Fixes: #2703 Fixes: #2710 Closes: #4153 Closes: #4232 Co-authored-by: Kevin (Kun) Kassimo Qian --- cli/js/error_stack.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) (limited to 'cli/js/error_stack.ts') diff --git a/cli/js/error_stack.ts b/cli/js/error_stack.ts index 97e9f68de..bab179e48 100644 --- a/cli/js/error_stack.ts +++ b/cli/js/error_stack.ts @@ -140,7 +140,7 @@ function callSiteToString(callSite: CallSite): string { result += "async "; } if (isPromiseAll) { - result += `Promise.all (index ${callSite.getPromiseIndex})`; + result += `Promise.all (index ${callSite.getPromiseIndex()})`; return result; } if (isMethodCall) { @@ -163,11 +163,52 @@ function callSiteToString(callSite: CallSite): string { return result; } +interface CallSiteEval { + this: unknown; + typeName: string; + function: Function; + functionName: string; + methodName: string; + fileName: string; + lineNumber: number | null; + columnNumber: number | null; + evalOrigin: string | null; + isToplevel: boolean; + 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(), + }; +} + function prepareStackTrace( error: Error, structuredStackTrace: CallSite[] ): string { - return ( + Object.defineProperty(error, "__callSiteEvals", { value: [] }); + const errorString = `${error.name}: ${error.message}\n` + structuredStackTrace .map( @@ -188,9 +229,18 @@ function prepareStackTrace( return callSite; } ) - .map((callSite): string => ` at ${callSiteToString(callSite)}`) - .join("\n") - ); + .map((callSite): string => { + const callSiteEv = Object.freeze(evaluateCallSite(callSite)); + if (callSiteEv.lineNumber != null && callSiteEv.columnNumber != null) { + // @ts-ignore + error["__callSiteEvals"].push(callSiteEv); + } + return ` at ${callSiteToString(callSite)}`; + }) + .join("\n"); + // @ts-ignore + Object.freeze(error["__callSiteEvals"]); + return errorString; } // @internal -- cgit v1.2.3