Skip to content

Commit

Permalink
lib: fix validateport error message when allowZero is false
Browse files Browse the repository at this point in the history
PR-URL: #32861
Fixes: #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>
  • Loading branch information
rickyes authored and targos committed May 13, 2020
1 parent d50fe6c commit 6356ad4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/internal/errors.js
Expand Up @@ -1310,8 +1310,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',
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/validators.js
Expand Up @@ -179,7 +179,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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-connect.js
Expand Up @@ -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'
});
});

0 comments on commit 6356ad4

Please sign in to comment.