diff --git a/lib/internal/errors.js b/lib/internal/errors.js index f6aee89dca63b7..684865c0a09b7b 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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(). * @@ -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 @@ -438,6 +473,7 @@ module.exports = { errnoException, exceptionWithHostPort, uvException, + uvExceptionWithHostPort, isStackOverflowError, getMessage, SystemError, diff --git a/lib/net.js b/lib/net.js index fdf7e8a1d0f910..f0b676633eefe0 100644 --- a/lib/net.js +++ b/lib/net.js @@ -85,7 +85,11 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); let cluster; let dns; -const { errnoException, exceptionWithHostPort } = errors; +const { + errnoException, + exceptionWithHostPort, + uvExceptionWithHostPort +} = errors; const { kTimeout, @@ -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; } @@ -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], diff --git a/lib/util.js b/lib/util.js index d1a483cf17bff7..b874e0aa024cb5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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,