Skip to content

Fix NodeError formatting to include code #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ function wrapCallSite(frame, state) {
return frame;
}

var kIsNodeError = undefined;
try {
// Get a deliberate ERR_INVALID_ARG_TYPE
// TODO is there a better way to reliably get an instance of NodeError?
path.resolve(123);
} catch(e) {
const symbols = Object.getOwnPropertySymbols(e);
const symbol = symbols.find(function (s) {return s.toString().indexOf('kIsNodeError') >= 0});
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) {
Expand All @@ -414,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 = [];
Expand Down Expand Up @@ -469,11 +494,10 @@ function printErrorAndExit (error) {
}

if (source) {
console.error();
console.error(source);
}

console.error(error.stack);
console.error(error);
process.exit(1);
}

Expand Down