diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 0682b58af61e17..c4e9fb3d59b4a3 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1318,8 +1318,12 @@ E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error); E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error); E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer', TypeError); -E('ERR_SOCKET_BAD_PORT', - '%s should be >= 0 and < 65536. Received %s.', RangeError); +E('ERR_SOCKET_BAD_PORT', (name, port, allowZero = true) => { + assert(typeof allowZero === 'boolean', + "The 'allowZero' argument must be of type boolean."); + const operator = allowZero ? '>=' : '>'; + return `${name} should be ${operator} 0 and < 65536. Received ${port}.`; +}, RangeError); E('ERR_SOCKET_BAD_TYPE', 'Bad socket type specified. Valid types are: udp4, udp6', TypeError); E('ERR_SOCKET_BUFFER_SIZE', diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 790c923386fa91..deab53d4b8221e 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -190,7 +190,7 @@ function validatePort(port, name = 'Port', { allowZero = true } = {}) { +port !== (+port >>> 0) || port > 0xFFFF || (port === 0 && !allowZero)) { - throw new ERR_SOCKET_BAD_PORT(name, port); + throw new ERR_SOCKET_BAD_PORT(name, port, allowZero); } return port | 0; } diff --git a/test/parallel/test-dgram-connect.js b/test/parallel/test-dgram-connect.js index a9815f439a731a..30e817f344a42c 100644 --- a/test/parallel/test-dgram-connect.js +++ b/test/parallel/test-dgram-connect.js @@ -60,7 +60,7 @@ assert.throws(() => { client.connect(port); }, { name: 'RangeError', - message: /^Port should be >= 0 and < 65536/, + message: /^Port should be > 0 and < 65536/, code: 'ERR_SOCKET_BAD_PORT' }); });