Skip to content

Commit 1d0b249

Browse files
rickyesaddaleax
authored andcommitted
lib: fix validateport error message when allowZero is false
PR-URL: #32861 Fixes: #32857 Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent aa9708e commit 1d0b249

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

lib/internal/errors.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,12 @@ E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);
13181318
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
13191319
E('ERR_SOCKET_BAD_BUFFER_SIZE',
13201320
'Buffer size must be a positive integer', TypeError);
1321-
E('ERR_SOCKET_BAD_PORT',
1322-
'%s should be >= 0 and < 65536. Received %s.', RangeError);
1321+
E('ERR_SOCKET_BAD_PORT', (name, port, allowZero = true) => {
1322+
assert(typeof allowZero === 'boolean',
1323+
"The 'allowZero' argument must be of type boolean.");
1324+
const operator = allowZero ? '>=' : '>';
1325+
return `${name} should be ${operator} 0 and < 65536. Received ${port}.`;
1326+
}, RangeError);
13231327
E('ERR_SOCKET_BAD_TYPE',
13241328
'Bad socket type specified. Valid types are: udp4, udp6', TypeError);
13251329
E('ERR_SOCKET_BUFFER_SIZE',

lib/internal/validators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function validatePort(port, name = 'Port', { allowZero = true } = {}) {
190190
+port !== (+port >>> 0) ||
191191
port > 0xFFFF ||
192192
(port === 0 && !allowZero)) {
193-
throw new ERR_SOCKET_BAD_PORT(name, port);
193+
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
194194
}
195195
return port | 0;
196196
}

test/parallel/test-dgram-connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ assert.throws(() => {
6060
client.connect(port);
6161
}, {
6262
name: 'RangeError',
63-
message: /^Port should be >= 0 and < 65536/,
63+
message: /^Port should be > 0 and < 65536/,
6464
code: 'ERR_SOCKET_BAD_PORT'
6565
});
6666
});

0 commit comments

Comments
 (0)