Skip to content

Commit 6356ad4

Browse files
rickyestargos
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 d50fe6c commit 6356ad4

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

lib/internal/errors.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1310,8 +1310,12 @@ E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);
13101310
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
13111311
E('ERR_SOCKET_BAD_BUFFER_SIZE',
13121312
'Buffer size must be a positive integer', TypeError);
1313-
E('ERR_SOCKET_BAD_PORT',
1314-
'%s should be >= 0 and < 65536. Received %s.', RangeError);
1313+
E('ERR_SOCKET_BAD_PORT', (name, port, allowZero = true) => {
1314+
assert(typeof allowZero === 'boolean',
1315+
"The 'allowZero' argument must be of type boolean.");
1316+
const operator = allowZero ? '>=' : '>';
1317+
return `${name} should be ${operator} 0 and < 65536. Received ${port}.`;
1318+
}, RangeError);
13151319
E('ERR_SOCKET_BAD_TYPE',
13161320
'Bad socket type specified. Valid types are: udp4, udp6', TypeError);
13171321
E('ERR_SOCKET_BUFFER_SIZE',

lib/internal/validators.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function validatePort(port, name = 'Port', { allowZero = true } = {}) {
179179
+port !== (+port >>> 0) ||
180180
port > 0xFFFF ||
181181
(port === 0 && !allowZero)) {
182-
throw new ERR_SOCKET_BAD_PORT(name, port);
182+
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
183183
}
184184
return port | 0;
185185
}

test/parallel/test-dgram-connect.js

+1-1
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)