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

net: throw error if port does not exist in options #22085

Prev Previous commit
Next Next commit
net: use ERR_INVALID_OPT_VALUE instead of ERR_PROPERTY_NOT_IN_OBJECT
  • Loading branch information
yanivfriedensohn authored and Trott committed Aug 22, 2018
commit 54fc80f90b67ceefc1a8044f0f6b2056436f7e3d
11 changes: 7 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const {
ERR_INVALID_FD_TYPE,
ERR_INVALID_IP_ADDRESS,
ERR_INVALID_OPT_VALUE,
ERR_PROPERTY_NOT_IN_OBJECT,
ERR_SERVER_ALREADY_LISTEN,
ERR_SERVER_NOT_RUNNING,
ERR_SOCKET_BAD_PORT,
Expand Down Expand Up @@ -1497,11 +1496,15 @@ Server.prototype.listen = function(...args) {
return this;
}

if (typeof options === 'object' && !('port' in options)) {
throw new ERR_PROPERTY_NOT_IN_OBJECT('port', 'options');
let additionalMessage;
if (typeof options === 'object' &&
!(('port' in options) || ('path' in options))) {

additionalMessage = '"port" or "path" must be specified in options.';
}

throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options),
additionalMessage);
};

function lookupAndListen(self, port, address, backlog, exclusive) {
Expand Down
21 changes: 6 additions & 15 deletions test/parallel/test-net-server-listen-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,12 @@ const listenOnPort = [
net.createServer().listen(options, common.mustNotCall());
};

if (typeof options === 'object' && !('port' in options)) {
common.expectsError(block,
{
code: 'ERR_PROPERTY_NOT_IN_OBJECT',
type: TypeError,
message: 'port does not exist in options',
});
} else {
common.expectsError(block,
{
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /^The value "{.*}" is invalid for option "options"$/
});
}
common.expectsError(block,
{
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/,
});
}

shouldFailToListen(false, { port: false });
Expand Down