Skip to content

Improving error messages description #22983

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
40 changes: 38 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,43 @@ function uvException(ctx) {
return err;
}

/**
* This creates an error compatible with errors produced in the C++
* This function should replace the deprecated `uvException(ctx)` function.
*
* @param {number} err - A libuv error number
* @param {string} syscall
* @param {string} address
* @param {number} [port]
* @param {string} [additional]
* @returns {Error}
*/
function uvExceptionWithHostPort(err, syscall, address, port, additional) {
const [ code, uvmsg ] = errmap.get(err);
const message = `${code}: ${uvmsg}, ${syscall}`;
let details = '';
if (port && port > 0) {
details = ` ${address}:${port}`;
} else if (address) {
details = ` ${address}`;
}
if (additional) {
details += ` - Local (${additional})`;
}

// eslint-disable-next-line no-restricted-syntax
const ex = new Error(`${message} ${details}`);
ex.code = code;
ex.syscall = syscall;
ex.address = address;
if (port) {
ex.port = port;
}

Error.captureStackTrace(ex, uvExceptionWithHostPort);
return ex;
}

/**
* This used to be util._errnoException().
*
Expand Down Expand Up @@ -315,8 +352,6 @@ function errnoException(err, syscall, original) {
}

/**
* This used to be util._exceptionWithHostPort().
*
* @param {number} err - A libuv error number
* @param {string} syscall
* @param {string} address
Expand Down Expand Up @@ -438,6 +473,7 @@ module.exports = {
errnoException,
exceptionWithHostPort,
uvException,
uvExceptionWithHostPort,
isStackOverflowError,
getMessage,
SystemError,
Expand Down
10 changes: 7 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
let cluster;
let dns;

const { errnoException, exceptionWithHostPort } = errors;
const {
errnoException,
exceptionWithHostPort,
uvExceptionWithHostPort
} = errors;

const {
kTimeout,
Expand Down Expand Up @@ -1267,7 +1271,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
rval = createServerHandle(address, port, addressType, fd);

if (typeof rval === 'number') {
var error = exceptionWithHostPort(rval, 'listen', address, port);
var error = uvExceptionWithHostPort(rval, 'listen', address, port);
process.nextTick(emitErrorNT, this, error);
return;
}
Expand All @@ -1284,7 +1288,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
var err = this._handle.listen(backlog || 511);

if (err) {
var ex = exceptionWithHostPort(err, 'listen', address, port);
var ex = uvExceptionWithHostPort(err, 'listen', address, port);
this._handle.close();
this._handle = null;
defaultTriggerAsyncIdScope(this[async_id_symbol],
Expand Down
1 change: 0 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,6 @@ function getSystemErrorName(err) {
// Keep the `exports =` so that various functions can still be monkeypatched
module.exports = exports = {
_errnoException: errors.errnoException,
_exceptionWithHostPort: errors.exceptionWithHostPort,
_extend,
callbackify,
debuglog,
Expand Down