Skip to content

Commit b5ac0f0

Browse files
committed
lib: fix validateport error message when allowZero is false
1 parent 3f5142d commit b5ac0f0

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
@@ -1308,8 +1308,12 @@ E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);
13081308
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
13091309
E('ERR_SOCKET_BAD_BUFFER_SIZE',
13101310
'Buffer size must be a positive integer', TypeError);
1311-
E('ERR_SOCKET_BAD_PORT',
1312-
'%s should be >= 0 and < 65536. Received %s.', RangeError);
1311+
E('ERR_SOCKET_BAD_PORT', (name, port, allowZero = true) => {
1312+
assert(typeof allowZero === 'boolean',
1313+
"The 'allowZero' argument must be of type boolean.");
1314+
const operator = allowZero ? '>=' : '>';
1315+
return `${name} should be ${operator} 0 and < 65536. Received ${port}.`;
1316+
}, RangeError);
13131317
E('ERR_SOCKET_BAD_TYPE',
13141318
'Bad socket type specified. Valid types are: udp4, udp6', TypeError);
13151319
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)