From 8a2a0e968a2dfa98b67701c2d446e977663cc9f9 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 19 Jul 2021 18:26:00 -0400 Subject: [PATCH 1/5] Initial implementation for review --- source-map-support.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index 4459386..2332d6f 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -408,6 +408,17 @@ function wrapCallSite(frame, state) { return frame; } +let kIsNodeError = undefined; +try { + // Get a deliberate ERR_INVALID_ARG_TYPE + // TODO is there a better way to reliably get an instance of NodeError? + new Buffer(); +} catch(e) { + const symbols = Object.getOwnPropertySymbols(e); + const symbol = symbols.find(s => s.toString().indexOf('kIsNodeError') >= 0); + if(symbol) kIsNodeError = symbol; +} + // This function is part of the V8 stack trace API, for more info see: // https://v8.dev/docs/stack-trace-api function prepareStackTrace(error, stack) { @@ -416,9 +427,21 @@ function prepareStackTrace(error, stack) { sourceMapCache = {}; } - var name = error.name || 'Error'; - var message = error.message || ''; - var errorString = name + ": " + message; + // node gives its own errors special treatment. Mimic that behavior + // https://github.com/nodejs/node/blob/3cbaabc4622df1b4009b9d026a1a970bdbae6e89/lib/internal/errors.js#L118-L128 + // https://github.com/nodejs/node/pull/39182 + var errorString; + if (kIsNodeError) { + if(kIsNodeError in error) { + errorString = `${error.name} [${error.code}]: ${error.message}`; + } else { + errorString = ErrorPrototypeToString(error); + } + } else { + var name = error.name || 'Error'; + var message = error.message || ''; + errorString = name + ": " + message; + } var state = { nextPosition: null, curPosition: null }; var processedStack = []; @@ -471,11 +494,10 @@ function printErrorAndExit (error) { } if (source) { - console.error(); console.error(source); } - console.error(error.stack); + console.error(error); process.exit(1); } From b731e0261f315ff9f210766707c00c684c1ae4e9 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 19 Jul 2021 20:29:24 -0400 Subject: [PATCH 2/5] fix --- source-map-support.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source-map-support.js b/source-map-support.js index 2332d6f..7652f7a 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -412,7 +412,7 @@ let kIsNodeError = undefined; try { // Get a deliberate ERR_INVALID_ARG_TYPE // TODO is there a better way to reliably get an instance of NodeError? - new Buffer(); + path.resolve(123); } catch(e) { const symbols = Object.getOwnPropertySymbols(e); const symbol = symbols.find(s => s.toString().indexOf('kIsNodeError') >= 0); From db624a375bf5d15e5f6df0efdabebbaa65ad29a8 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 19 Jul 2021 20:37:58 -0400 Subject: [PATCH 3/5] add missing primordial --- source-map-support.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source-map-support.js b/source-map-support.js index 7652f7a..9c17d1a 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -419,6 +419,8 @@ try { if(symbol) kIsNodeError = symbol; } +const ErrorPrototypeToString = (err) =>Error.prototype.toString.call(err); + // This function is part of the V8 stack trace API, for more info see: // https://v8.dev/docs/stack-trace-api function prepareStackTrace(error, stack) { From 794bdb062c486d401436af5b673342bcfd007d02 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 19 Jul 2021 20:40:25 -0400 Subject: [PATCH 4/5] fix --- source-map-support.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source-map-support.js b/source-map-support.js index 9c17d1a..51f1f30 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -408,7 +408,7 @@ function wrapCallSite(frame, state) { return frame; } -let kIsNodeError = undefined; +var kIsNodeError = undefined; try { // Get a deliberate ERR_INVALID_ARG_TYPE // TODO is there a better way to reliably get an instance of NodeError? From d799caf476111a8b01910eaeaa9d590733dccbb9 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 19 Jul 2021 20:44:54 -0400 Subject: [PATCH 5/5] fix --- source-map-support.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source-map-support.js b/source-map-support.js index 51f1f30..98b3d52 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -415,7 +415,7 @@ try { path.resolve(123); } catch(e) { const symbols = Object.getOwnPropertySymbols(e); - const symbol = symbols.find(s => s.toString().indexOf('kIsNodeError') >= 0); + const symbol = symbols.find(function (s) {return s.toString().indexOf('kIsNodeError') >= 0}); if(symbol) kIsNodeError = symbol; }