Skip to content

Commit 7f55349

Browse files
matzavinosjoyeecheung
matzavinos
authored andcommitted
net: convert to using internal/errors
Covert lib/net.js over to using lib/internal/errors.js - Replace thrown errors in lib/net.js with errors from lib/internal/errors. The ERR_INVALID_OPT_VALUE error have been used in the Server.prototype.listen() method - Update tests according to the above modifications PR-URL: #14782 Refs: #11273 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 2b76b5d commit 7f55349

15 files changed

+168
-125
lines changed

doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,11 @@ Used when `hostname` can not be parsed from a provided URL.
940940

941941
Used when a file descriptor ('fd') is not valid (e.g. it has a negative value).
942942

943+
<a id="ERR_INVALID_FD_TYPE"></a>
944+
### ERR_INVALID_FD_TYPE
945+
946+
Used when a file descriptor ('fd') type is not valid.
947+
943948
<a id="ERR_INVALID_FILE_URL_HOST"></a>
944949
### ERR_INVALID_FILE_URL_HOST
945950

lib/dgram.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ Socket.prototype.send = function(buffer,
417417

418418
port = port >>> 0;
419419
if (port === 0 || port > 65535)
420-
throw new errors.RangeError('ERR_SOCKET_BAD_PORT');
420+
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port);
421421

422422
// Normalize callback so it's either a function or undefined but not anything
423423
// else.

lib/dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function lookupService(host, port, callback) {
216216
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'host', host);
217217

218218
if (!isLegalPort(port))
219-
throw new errors.RangeError('ERR_SOCKET_BAD_PORT');
219+
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port);
220220

221221
if (typeof callback !== 'function')
222222
throw new errors.TypeError('ERR_INVALID_CALLBACK');

lib/internal/errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ E('ERR_INVALID_CURSOR_POS',
254254
'Cannot set cursor row without setting its column');
255255
E('ERR_INVALID_DOMAIN_NAME', 'Unable to determine the domain name');
256256
E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
257+
E('ERR_INVALID_FD_TYPE', 'Unsupported fd type: %s');
257258
E('ERR_INVALID_FILE_URL_HOST',
258259
'File URL host must be "localhost" or empty on %s');
259260
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
@@ -301,7 +302,7 @@ E('ERR_SERVER_ALREADY_LISTEN',
301302
'Listen method has been called more than once without closing.');
302303
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
303304
E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer');
304-
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
305+
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536. Received %s.');
305306
E('ERR_SOCKET_BAD_TYPE',
306307
'Bad socket type specified. Valid types are: udp4, udp6');
307308
E('ERR_SOCKET_BUFFER_SIZE',

lib/net.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ const normalizedArgsSymbol = internalNet.normalizedArgsSymbol;
6161
function noop() {}
6262

6363
function createHandle(fd) {
64-
var type = TTYWrap.guessHandleType(fd);
64+
const type = TTYWrap.guessHandleType(fd);
6565
if (type === 'PIPE') return new Pipe();
6666
if (type === 'TCP') return new TCP();
67-
throw new TypeError('Unsupported fd type: ' + type);
67+
throw new errors.TypeError('ERR_INVALID_FD_TYPE', type);
6868
}
6969

7070

@@ -695,8 +695,10 @@ protoGetter('localPort', function localPort() {
695695

696696
Socket.prototype.write = function(chunk, encoding, cb) {
697697
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
698-
throw new TypeError(
699-
'Invalid data, chunk must be a string or buffer, not ' + typeof chunk);
698+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
699+
'chunk',
700+
['string', 'Buffer'],
701+
chunk);
700702
}
701703
return stream.Duplex.prototype.write.apply(this, arguments);
702704
};
@@ -1035,21 +1037,25 @@ function lookupAndConnect(self, options) {
10351037
var localPort = options.localPort;
10361038

10371039
if (localAddress && !cares.isIP(localAddress)) {
1038-
throw new TypeError('"localAddress" option must be a valid IP: ' +
1039-
localAddress);
1040+
throw new errors.TypeError('ERR_INVALID_IP_ADDRESS', localAddress);
10401041
}
10411042

10421043
if (localPort && typeof localPort !== 'number') {
1043-
throw new TypeError('"localPort" option should be a number: ' + localPort);
1044+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
1045+
'options.localPort',
1046+
'number',
1047+
localPort);
10441048
}
10451049

10461050
if (typeof port !== 'undefined') {
10471051
if (typeof port !== 'number' && typeof port !== 'string') {
1048-
throw new TypeError('"port" option should be a number or string: ' +
1049-
port);
1052+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
1053+
'options.port',
1054+
['number', 'string'],
1055+
port);
10501056
}
10511057
if (!isLegalPort(port)) {
1052-
throw new RangeError('"port" option should be >= 0 and < 65536: ' + port);
1058+
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port);
10531059
}
10541060
}
10551061
port |= 0;
@@ -1065,7 +1071,10 @@ function lookupAndConnect(self, options) {
10651071
}
10661072

10671073
if (options.lookup && typeof options.lookup !== 'function')
1068-
throw new TypeError('"lookup" option should be a function');
1074+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
1075+
'options.lookup',
1076+
'function',
1077+
options.lookup);
10691078

10701079
var dnsopts = {
10711080
family: options.family,
@@ -1207,7 +1216,10 @@ function Server(options, connectionListener) {
12071216
this.on('connection', connectionListener);
12081217
}
12091218
} else {
1210-
throw new TypeError('options must be an object');
1219+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
1220+
'options',
1221+
'object',
1222+
options);
12111223
}
12121224

12131225
this._connections = 0;
@@ -1465,7 +1477,7 @@ Server.prototype.listen = function(...args) {
14651477
var backlog;
14661478
if (typeof options.port === 'number' || typeof options.port === 'string') {
14671479
if (!isLegalPort(options.port)) {
1468-
throw new RangeError('"port" argument must be >= 0 and < 65536');
1480+
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', options.port);
14691481
}
14701482
backlog = options.backlog || backlogFromArgs;
14711483
// start TCP server listening on host:port
@@ -1490,7 +1502,9 @@ Server.prototype.listen = function(...args) {
14901502
return this;
14911503
}
14921504

1493-
throw new Error('Invalid listen argument: ' + util.inspect(options));
1505+
throw new errors.Error('ERR_INVALID_OPT_VALUE',
1506+
'options',
1507+
util.inspect(options));
14941508
};
14951509

14961510
function lookupAndListen(self, port, address, backlog, exclusive) {

test/parallel/test-dns.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,23 @@ assert.throws(() => {
220220
message: `The value "${invalidHost}" is invalid for option "host"`
221221
}));
222222

223-
const badPortMsg = common.expectsError({
224-
code: 'ERR_SOCKET_BAD_PORT',
225-
type: RangeError,
226-
message: 'Port should be > 0 and < 65536'
227-
}, 4);
228-
assert.throws(() => dns.lookupService('0.0.0.0', null, common.mustNotCall()),
229-
badPortMsg);
230-
231-
assert.throws(
232-
() => dns.lookupService('0.0.0.0', undefined, common.mustNotCall()),
233-
badPortMsg
234-
);
235-
236-
assert.throws(() => dns.lookupService('0.0.0.0', 65538, common.mustNotCall()),
237-
badPortMsg);
238-
239-
assert.throws(() => dns.lookupService('0.0.0.0', 'test', common.mustNotCall()),
240-
badPortMsg);
223+
const portErr = (port) => {
224+
common.expectsError(
225+
() => {
226+
dns.lookupService('0.0.0.0', port, common.mustNotCall());
227+
},
228+
{
229+
code: 'ERR_SOCKET_BAD_PORT',
230+
message:
231+
`Port should be > 0 and < 65536. Received ${port}.`,
232+
type: RangeError
233+
}
234+
);
235+
};
236+
portErr(null);
237+
portErr(undefined);
238+
portErr(65538);
239+
portErr('test');
241240

242241
assert.throws(() => {
243242
dns.lookupService('0.0.0.0', 80, null);

test/parallel/test-internal-errors.js

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ assert.strictEqual(
216216
errors.message('ERR_INVALID_ARG_TYPE', [['a', 'b', 'c'], 'not d']),
217217
'The "a", "b", "c" arguments must not be of type d');
218218

219+
// Test ERR_INVALID_FD_TYPE
220+
assert.strictEqual(errors.message('ERR_INVALID_FD_TYPE', ['a']),
221+
'Unsupported fd type: a');
222+
219223
// Test ERR_INVALID_URL_SCHEME
220224
assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', ['file']),
221225
'The URL must be of scheme file');
@@ -246,6 +250,10 @@ assert.throws(
246250
message: /^At least one arg needs to be specified$/
247251
}));
248252

253+
// Test ERR_SOCKET_BAD_PORT
254+
assert.strictEqual(
255+
errors.message('ERR_SOCKET_BAD_PORT', [0]),
256+
'Port should be > 0 and < 65536. Received 0.');
249257

250258
// Test ERR_TLS_CERT_ALTNAME_INVALID
251259
assert.strictEqual(

test/parallel/test-net-connect-options-port.js

+35-30
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,33 @@ const net = require('net');
2727

2828
// Test wrong type of ports
2929
{
30-
function portTypeError(opt) {
31-
const prefix = '"port" option should be a number or string: ';
32-
const cleaned = opt.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&');
33-
return new RegExp(`^TypeError: ${prefix}${cleaned}$`);
34-
}
35-
36-
syncFailToConnect(true, portTypeError('true'));
37-
syncFailToConnect(false, portTypeError('false'));
38-
syncFailToConnect([], portTypeError(''), true);
39-
syncFailToConnect({}, portTypeError('[object Object]'), true);
40-
syncFailToConnect(null, portTypeError('null'));
30+
const portTypeError = common.expectsError({
31+
code: 'ERR_INVALID_ARG_TYPE',
32+
type: TypeError
33+
}, 96);
34+
35+
syncFailToConnect(true, portTypeError);
36+
syncFailToConnect(false, portTypeError);
37+
syncFailToConnect([], portTypeError, true);
38+
syncFailToConnect({}, portTypeError, true);
39+
syncFailToConnect(null, portTypeError);
4140
}
4241

4342
// Test out of range ports
4443
{
45-
function portRangeError(opt) {
46-
const prefix = '"port" option should be >= 0 and < 65536: ';
47-
const cleaned = opt.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&');
48-
return new RegExp(`^RangeError: ${prefix}${cleaned}$`);
49-
}
50-
51-
syncFailToConnect('', portRangeError(''));
52-
syncFailToConnect(' ', portRangeError(' '));
53-
syncFailToConnect('0x', portRangeError('0x'), true);
54-
syncFailToConnect('-0x1', portRangeError('-0x1'), true);
55-
syncFailToConnect(NaN, portRangeError('NaN'));
56-
syncFailToConnect(Infinity, portRangeError('Infinity'));
57-
syncFailToConnect(-1, portRangeError('-1'));
58-
syncFailToConnect(65536, portRangeError('65536'));
44+
const portRangeError = common.expectsError({
45+
code: 'ERR_SOCKET_BAD_PORT',
46+
type: RangeError
47+
}, 168);
48+
49+
syncFailToConnect('', portRangeError);
50+
syncFailToConnect(' ', portRangeError);
51+
syncFailToConnect('0x', portRangeError, true);
52+
syncFailToConnect('-0x1', portRangeError, true);
53+
syncFailToConnect(NaN, portRangeError);
54+
syncFailToConnect(Infinity, portRangeError);
55+
syncFailToConnect(-1, portRangeError);
56+
syncFailToConnect(65536, portRangeError);
5957
}
6058

6159
// Test invalid hints
@@ -129,33 +127,40 @@ function doConnect(args, getCb) {
129127
];
130128
}
131129

132-
function syncFailToConnect(port, regexp, optOnly) {
130+
function syncFailToConnect(port, assertErr, optOnly) {
133131
if (!optOnly) {
134132
// connect(port, cb) and connect(port)
135133
const portArgBlocks = doConnect([port], () => common.mustNotCall());
136134
for (const block of portArgBlocks) {
137-
assert.throws(block, regexp, `${block.name}(${port})`);
135+
assert.throws(block,
136+
assertErr,
137+
`${block.name}(${port})`);
138138
}
139139

140140
// connect(port, host, cb) and connect(port, host)
141141
const portHostArgBlocks = doConnect([port, 'localhost'],
142142
() => common.mustNotCall());
143143
for (const block of portHostArgBlocks) {
144-
assert.throws(block, regexp, `${block.name}(${port}, 'localhost')`);
144+
assert.throws(block,
145+
assertErr,
146+
`${block.name}(${port}, 'localhost')`);
145147
}
146148
}
147149
// connect({port}, cb) and connect({port})
148150
const portOptBlocks = doConnect([{ port }],
149151
() => common.mustNotCall());
150152
for (const block of portOptBlocks) {
151-
assert.throws(block, regexp, `${block.name}({port: ${port}})`);
153+
assert.throws(block,
154+
assertErr,
155+
`${block.name}({port: ${port}})`);
152156
}
153157

154158
// connect({port, host}, cb) and connect({port, host})
155159
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }],
156160
() => common.mustNotCall());
157161
for (const block of portHostOptBlocks) {
158-
assert.throws(block, regexp,
162+
assert.throws(block,
163+
assertErr,
159164
`${block.name}({port: ${port}, host: 'localhost'})`);
160165
}
161166
}

test/parallel/test-net-localerror.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,24 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
24-
const assert = require('assert');
23+
const common = require('../common');
2524
const net = require('net');
2625

27-
// Using port 0 as localPort / localAddress is already invalid.
26+
const connect = (opts, code, type) => {
27+
common.expectsError(
28+
() => net.connect(opts),
29+
{ code, type }
30+
);
31+
};
32+
2833
connect({
2934
host: 'localhost',
3035
port: 0,
31-
localPort: 'foobar',
32-
}, /^TypeError: "localPort" option should be a number: foobar$/);
36+
localAddress: 'foobar',
37+
}, 'ERR_INVALID_IP_ADDRESS', TypeError);
3338

3439
connect({
3540
host: 'localhost',
3641
port: 0,
37-
localAddress: 'foobar',
38-
}, /^TypeError: "localAddress" option must be a valid IP: foobar$/);
39-
40-
function connect(opts, msg) {
41-
assert.throws(() => {
42-
net.connect(opts);
43-
}, msg);
44-
}
42+
localPort: 'foobar',
43+
}, 'ERR_INVALID_ARG_TYPE', TypeError);

test/parallel/test-net-options-lookup.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

6-
const expectedError = /^TypeError: "lookup" option should be a function$/;
7-
86
['foobar', 1, {}, []].forEach((input) => connectThrows(input));
97

108
// Using port 0 as lookup is emitted before connecting.
@@ -17,7 +15,10 @@ function connectThrows(input) {
1715

1816
assert.throws(() => {
1917
net.connect(opts);
20-
}, expectedError);
18+
}, common.expectsError({
19+
code: 'ERR_INVALID_ARG_TYPE',
20+
type: TypeError
21+
}));
2122
}
2223

2324
connectDoesNotThrow(() => {});

0 commit comments

Comments
 (0)