Skip to content

Commit 8a2a0e9

Browse files
committed
Initial implementation for review
1 parent d058745 commit 8a2a0e9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

source-map-support.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,17 @@ function wrapCallSite(frame, state) {
408408
return frame;
409409
}
410410

411+
let kIsNodeError = undefined;
412+
try {
413+
// Get a deliberate ERR_INVALID_ARG_TYPE
414+
// TODO is there a better way to reliably get an instance of NodeError?
415+
new Buffer();
416+
} catch(e) {
417+
const symbols = Object.getOwnPropertySymbols(e);
418+
const symbol = symbols.find(s => s.toString().indexOf('kIsNodeError') >= 0);
419+
if(symbol) kIsNodeError = symbol;
420+
}
421+
411422
// This function is part of the V8 stack trace API, for more info see:
412423
// https://v8.dev/docs/stack-trace-api
413424
function prepareStackTrace(error, stack) {
@@ -416,9 +427,21 @@ function prepareStackTrace(error, stack) {
416427
sourceMapCache = {};
417428
}
418429

419-
var name = error.name || 'Error';
420-
var message = error.message || '';
421-
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+
}
422445

423446
var state = { nextPosition: null, curPosition: null };
424447
var processedStack = [];
@@ -471,11 +494,10 @@ function printErrorAndExit (error) {
471494
}
472495

473496
if (source) {
474-
console.error();
475497
console.error(source);
476498
}
477499

478-
console.error(error.stack);
500+
console.error(error);
479501
process.exit(1);
480502
}
481503

0 commit comments

Comments
 (0)