From f51f4e2aad97a6d863202eef3493757e9ce2c0c5 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sat, 11 Apr 2020 15:05:17 +0200 Subject: [PATCH] Make getStack to clean up event when error occurs --- src/base.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/base.ts b/src/base.ts index b068fc5..42ab320 100644 --- a/src/base.ts +++ b/src/base.ts @@ -391,25 +391,23 @@ export function withColor(color: Colors, value: any): ColorFormat { function getStack() { // Save original Error.prepareStackTrace let origPrepareStackTrace = (Error).prepareStackTrace; + try { + // Override with function that just returns `stack` + (Error).prepareStackTrace = function (_, stack) { + return stack; + }; + + // Create a new `Error`, which automatically gets `stack` + let err = new Error(); + + // Evaluate `err.stack`, which calls our new `Error.prepareStackTrace` + let stack: any[] = err.stack; - // Override with function that just returns `stack` - (Error).prepareStackTrace = function (_, stack) { return stack; - }; - - // Create a new `Error`, which automatically gets `stack` - let err = new Error(); - - // Evaluate `err.stack`, which calls our new `Error.prepareStackTrace` - let stack: any[] = err.stack; - - // Restore original `Error.prepareStackTrace` - (Error).prepareStackTrace = origPrepareStackTrace; - - // Remove superfluous function call on stack - stack.shift(); // getStack --> Error - - return stack; + } finally { + // Restore original `Error.prepareStackTrace` + (Error).prepareStackTrace = origPrepareStackTrace; + } } function baseName(path) {