Skip to content

Commit d2c3e7e

Browse files
committed
internal/net: use internal/errors.js
1 parent dfdd911 commit d2c3e7e

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

doc/api/errors.md

+12
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,17 @@ found [here][online].
562562
the connected party did not properly respond after a period of time. Usually
563563
encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()`
564564
was not properly called.
565+
566+
<a id="nodejs-error-codes"></a>
567+
## Node.js Error Codes
568+
569+
<a id="ERR_INVALID_PORT"></a>
570+
### ERR_INVALID_PORT
571+
572+
An error using the `'ERR_INVALID_PORT'` code is thrown specifically when an attempt
573+
is made to provide a network port which is not in the range of valid port numbers
574+
(0-65535)
575+
565576

566577
[`fs.readdir`]: fs.html#fs_fs_readdir_path_options_callback
567578
[`fs.readFileSync`]: fs.html#fs_fs_readfilesync_file_options
@@ -575,6 +586,7 @@ found [here][online].
575586
[domains]: domain.html
576587
[event emitter-based]: events.html#events_class_eventemitter
577588
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
589+
[Node.js Error Codes]: #nodejs-error-codes
578590
[online]: http://man7.org/linux/man-pages/man3/errno.3.html
579591
[stream-based]: stream.html
580592
[syscall]: http://man7.org/linux/man-pages/man2/syscall.2.html

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ module.exports = exports = {
8686
// Note: Please try to keep these in alphabetical order
8787
E('ERR_ASSERTION', (msg) => msg);
8888
// Add new errors from here...
89+
E('ERR_INVALID_PORT', (port) => {
90+
return `Port must be >= 0 and < 65536, got "${port}"`;
91+
});

lib/internal/net.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const errors = require('internal/errors');
4+
35
module.exports = { isLegalPort, assertPort };
46

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

1517
function assertPort(port) {
1618
if (typeof port !== 'undefined' && !isLegalPort(port))
17-
throw new RangeError('"port" argument must be >= 0 and < 65536');
19+
throw new errors.RangeError('ERR_INVALID_PORT', port);
1820
}

test/parallel/test-net-listen-port-option.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const common = require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

6+
const portRangeError = common.expectsError('ERR_INVALID_PORT', RangeError);
7+
68
function close() { this.close(); }
79

810
// From lib/net.js
@@ -36,7 +38,7 @@ listenVariants.forEach((listenVariant, i) => {
3638
return;
3739
}
3840
assert.throws(() => listenVariant(port, common.mustNotCall()),
39-
/"port" argument must be >= 0 and < 65536/i);
41+
portRangeError);
4042
});
4143

4244
[null, true, false].forEach((port) =>

test/parallel/test-regress-GH-5727.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44
const net = require('net');
55

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

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

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

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

0 commit comments

Comments
 (0)