lib: fix validateport error message when allowZero is false

PR-URL: https://github.com/nodejs/node/pull/32861
Fixes: https://github.com/nodejs/node/issues/32857
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
rickyes 2020-04-15 13:23:26 +08:00 committed by Anna Henningsen
parent aa9708e479
commit 1d0b24924f
No known key found for this signature in database
GPG Key ID: A94130F0BFC8EBE9
3 changed files with 8 additions and 4 deletions

View File

@ -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',

View File

@ -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;
}

View File

@ -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'
});
});