diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 656c7e2c6c5a5b..9f5e6d696c3043 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -286,6 +286,46 @@ function uvException(ctx) { return err; } +/** + * This creates an error compatible with errors produced in the C++ + * This function should replace the deprecated + * `exceptionWithHostPort()` 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 = `${syscall} ${code}: ${uvmsg}`; + 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.errno = code; + ex.syscall = syscall; + ex.address = address; + if (port) { + ex.port = port; + } + + Error.captureStackTrace(ex, uvExceptionWithHostPort); + return ex; +} + /** * This used to be util._errnoException(). * @@ -315,8 +355,9 @@ function errnoException(err, syscall, original) { } /** - * This used to be util._exceptionWithHostPort(). - * + * Deprecated, new function is `uvExceptionWithHostPort()` + * New function added the error description directly + * from C++. this method for backwards compatibility * @param {number} err - A libuv error number * @param {string} syscall * @param {string} address @@ -440,6 +481,7 @@ module.exports = { errnoException, exceptionWithHostPort, uvException, + uvExceptionWithHostPort, isStackOverflowError, getMessage, SystemError, diff --git a/lib/net.js b/lib/net.js index 03f52e2c4aabbb..cc048eccbbd85b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -83,7 +83,11 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); let cluster; let dns; -const { errnoException, exceptionWithHostPort } = errors; +const { + errnoException, + exceptionWithHostPort, + uvExceptionWithHostPort +} = errors; const { kTimeout, @@ -1266,7 +1270,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; } @@ -1283,7 +1287,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], diff --git a/test/parallel/test-net-server-listen-handle.js b/test/parallel/test-net-server-listen-handle.js index 5bf8451302fae9..72bdb482d3b4a3 100644 --- a/test/parallel/test-net-server-listen-handle.js +++ b/test/parallel/test-net-server-listen-handle.js @@ -148,7 +148,7 @@ if (!common.isWindows) { // Windows doesn't support {fd: } net.createServer() .listen({ fd }, common.mustNotCall()) .on('error', common.mustCall(function(err) { - assert.strictEqual(String(err), 'Error: listen EINVAL'); + assert.strictEqual(String(err), 'Error: listen EINVAL: invalid argument'); this.close(); })); }