Skip to content

Commit

Permalink
net: throw error if port does not exist in options
Browse files Browse the repository at this point in the history
Throw error ERR_PROPERTY_NOT_IN_OBJECT if the port property does not
exist in the options object argument when calling server.listen().

Refs: nodejs#16712
  • Loading branch information
yanivfriedensohn authored and Trott committed Aug 22, 2018
1 parent 36468ca commit 6a90ec5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,11 @@ A Node.js API was called in an unsupported manner, such as

A given value is out of the accepted range.

<a id="ERR_PROPERTY_NOT_IN_OBJECT"></a>
### ERR_PROPERTY_NOT_IN_OBJECT

A property does not exist in an object.

<a id="ERR_REQUIRE_ESM"></a>
### ERR_REQUIRE_ESM

Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ E('ERR_OUT_OF_RANGE',
msg += ` Received ${value}`;
return msg;
}, RangeError);
E('ERR_PROPERTY_NOT_IN_OBJECT', '%s does not exist in %s', TypeError);
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
'Script execution was interrupted by `SIGINT`', Error);
Expand Down
5 changes: 5 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ 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 @@ -1496,6 +1497,10 @@ Server.prototype.listen = function(...args) {
return this;
}

if (typeof options === 'object' && !('port' in options)) {
throw new ERR_PROPERTY_NOT_IN_OBJECT('port', 'options');
}

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

Expand Down
22 changes: 16 additions & 6 deletions test/parallel/test-net-server-listen-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ const listenOnPort = [
const block = () => {
net.createServer().listen(options, common.mustNotCall());
};
common.expectsError(block,
{
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /^The value "{.*}" is invalid for option "options"$/
});

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"$/
});
}
}

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

0 comments on commit 6a90ec5

Please sign in to comment.