Skip to content

Commit a7e3894

Browse files
authored
Merge pull request #3 from cspotcode/ab/fix-291
Fix NodeError formatting to include code
2 parents bf21f62 + 241fcc2 commit a7e3894

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

source-map-support.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,19 @@ function wrapCallSite(frame, state) {
406406
return frame;
407407
}
408408

409+
var kIsNodeError = undefined;
410+
try {
411+
// Get a deliberate ERR_INVALID_ARG_TYPE
412+
// TODO is there a better way to reliably get an instance of NodeError?
413+
path.resolve(123);
414+
} catch(e) {
415+
const symbols = Object.getOwnPropertySymbols(e);
416+
const symbol = symbols.find(function (s) {return s.toString().indexOf('kIsNodeError') >= 0});
417+
if(symbol) kIsNodeError = symbol;
418+
}
419+
420+
const ErrorPrototypeToString = (err) =>Error.prototype.toString.call(err);
421+
409422
// This function is part of the V8 stack trace API, for more info see:
410423
// https://v8.dev/docs/stack-trace-api
411424
function prepareStackTrace(error, stack) {
@@ -414,9 +427,21 @@ function prepareStackTrace(error, stack) {
414427
sourceMapCache = {};
415428
}
416429

417-
var name = error.name || 'Error';
418-
var message = error.message || '';
419-
var errorString = name + ": " + message;
430+
// node gives its own errors special treatment. Mimic that behavior
431+
// https://github.com/nodejs/node/blob/3cbaabc4622df1b4009b9d026a1a970bdbae6e89/lib/internal/errors.js#L118-L128
432+
// https://github.com/nodejs/node/pull/39182
433+
var errorString;
434+
if (kIsNodeError) {
435+
if(kIsNodeError in error) {
436+
errorString = `${error.name} [${error.code}]: ${error.message}`;
437+
} else {
438+
errorString = ErrorPrototypeToString(error);
439+
}
440+
} else {
441+
var name = error.name || 'Error';
442+
var message = error.message || '';
443+
errorString = name + ": " + message;
444+
}
420445

421446
var state = { nextPosition: null, curPosition: null };
422447
var processedStack = [];
@@ -469,11 +494,10 @@ function printErrorAndExit (error) {
469494
}
470495

471496
if (source) {
472-
console.error();
473497
console.error(source);
474498
}
475499

476-
console.error(error.stack);
500+
console.error(error);
477501
process.exit(1);
478502
}
479503

0 commit comments

Comments
 (0)