Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
lib: fix validateport error message when allowZero is false
  • Loading branch information
rickyes committed Apr 15, 2020
commit b5ac0f019ffed6ac36c8af072d3928227f2e65d3
8 changes: 6 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-connect.js
Original file line number Diff line number Diff line change
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'
});
});