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

errors,net: change internal/net.js to use internal/errors.js #11302

Closed
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
12 changes: 12 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ found [here][online].
the connected party did not properly respond after a period of time. Usually
encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()`
was not properly called.

<a id="nodejs-error-codes"></a>
## Node.js Error Codes

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

An error using the `'ERR_INVALID_PORT'` code is thrown specifically when an attempt
is made to provide a network port which is not in the range of valid port numbers
(0-65535)


[`fs.readdir`]: fs.html#fs_fs_readdir_path_options_callback
[`fs.readFileSync`]: fs.html#fs_fs_readfilesync_file_options
Expand All @@ -575,6 +586,7 @@ found [here][online].
[domains]: domain.html
[event emitter-based]: events.html#events_class_eventemitter
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
[Node.js Error Codes]: #nodejs-error-codes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be #nodejs_error_codes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell Yes, I realized it after a while. Shouldn't we let the tools take care of deciding the ids?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done it this way to ensure that the ID's remain constant even if the automatic link generation changes or the structure of the document is refactored. I much prefer the explicit headers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I am not sure. We lose consistency here.

[online]: http://man7.org/linux/man-pages/man3/errno.3.html
[stream-based]: stream.html
[syscall]: http://man7.org/linux/man-pages/man2/syscall.2.html
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ module.exports = exports = {
// Note: Please try to keep these in alphabetical order
E('ERR_ASSERTION', (msg) => msg);
// Add new errors from here...
E('ERR_INVALID_PORT', (port) => {
return `Port must be >= 0 and < 65536, got "${port}"`;
Copy link
Member

@joyeecheung joyeecheung Feb 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the block is unnecessary, we can just

(port) => `"port" argument must be >= 0 and < 65536, got ${port}`

EDIT: no need for the quotes around ${port}, that looks like a string but usually it's meant to be a number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the quotes in the case it is not a number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I keep the quotes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with keeping the quotes.

});
4 changes: 3 additions & 1 deletion lib/internal/net.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const errors = require('internal/errors');

module.exports = { isLegalPort, assertPort };

// Check that the port number is not NaN when coerced to a number,
Expand All @@ -14,5 +16,5 @@ function isLegalPort(port) {

function assertPort(port) {
if (typeof port !== 'undefined' && !isLegalPort(port))
throw new RangeError('"port" argument must be >= 0 and < 65536');
throw new errors.RangeError('ERR_INVALID_PORT', port);
}
4 changes: 3 additions & 1 deletion test/parallel/test-net-listen-port-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');

const portRangeError = common.expectsError('ERR_INVALID_PORT', RangeError);

function close() { this.close(); }

// From lib/net.js
Expand Down Expand Up @@ -36,7 +38,7 @@ listenVariants.forEach((listenVariant, i) => {
return;
}
assert.throws(() => listenVariant(port, common.mustNotCall()),
/"port" argument must be >= 0 and < 65536/i);
portRangeError);
});

[null, true, false].forEach((port) =>
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-regress-GH-5727.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');
const net = require('net');

const invalidPort = -1 >>> 0;
const errorMessage = /"port" argument must be >= 0 and < 65536/;
const portRangeError = common.expectsError('ERR_INVALID_PORT', RangeError);

net.Server().listen(common.PORT, function() {
const address = this.address();
Expand All @@ -17,14 +17,14 @@ net.Server().listen(common.PORT, function() {
// The first argument is a configuration object
assert.throws(() => {
net.Server().listen({ port: invalidPort }, common.mustNotCall());
}, errorMessage);
}, portRangeError);

// The first argument is the port, no IP given.
assert.throws(() => {
net.Server().listen(invalidPort, common.mustNotCall());
}, errorMessage);
}, portRangeError);

// The first argument is the port, the second an IP.
assert.throws(() => {
net.Server().listen(invalidPort, '0.0.0.0', common.mustNotCall());
}, errorMessage);
}, portRangeError);