Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix validateport error message when allowZero is false #32861

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/internal/errors.js
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
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
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'
});
});