Skip to content

Commit 5a4e982

Browse files
matzavinosjasnell
authored andcommitted
[fixup] lib/net: simplify err ERR_SOCKET_BAD_PORT
- simplify the ERR_SOCKET_BAD_PORT error definition in errors.js by replacing the cb arg with the string: 'Port should be > 0 and < 65536. Received %s.' - update the ERR_SOCKET_BAD_PORT test in test-internal-errors.js - update the ERR_SOCKET_BAD_PORT constructor call in lib/dgram.js and lib/dns to include the actual port as the second arg - update the already existing ERR_SOCKET_BAD_PORT message assertion in test-dns.js, to include the actual port.
1 parent 74d6b0b commit 5a4e982

File tree

5 files changed

+23
-33
lines changed

5 files changed

+23
-33
lines changed

lib/dgram.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,7 @@ E('ERR_SERVER_ALREADY_LISTEN',
267267
'Listen method has been called more than once without closing.');
268268
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
269269
E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer');
270-
E('ERR_SOCKET_BAD_PORT', (port) => {
271-
let portMsg = '';
272-
if (typeof port !== 'undefined' && port !== null) {
273-
portMsg = `:${port}`;
274-
}
275-
return `Port${portMsg} should be > 0 and < 65536`;
276-
});
270+
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536. Received %s.');
277271
E('ERR_SOCKET_BAD_TYPE',
278272
'Bad socket type specified. Valid types are: udp4, udp6');
279273
E('ERR_SOCKET_BUFFER_SIZE',

test/parallel/test-dns.js

Lines changed: 17 additions & 18 deletions
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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,9 @@ assert.throws(
242242
}));
243243

244244
// Test ERR_SOCKET_BAD_PORT
245-
assert.strictEqual(errors.message('ERR_SOCKET_BAD_PORT', []),
246-
'Port should be > 0 and < 65536');
247-
assert.strictEqual(errors.message('ERR_SOCKET_BAD_PORT', ['0']),
248-
'Port:0 should be > 0 and < 65536');
249-
assert.strictEqual(errors.message('ERR_SOCKET_BAD_PORT', [0]),
250-
'Port:0 should be > 0 and < 65536');
245+
assert.strictEqual(
246+
errors.message('ERR_SOCKET_BAD_PORT', [0]),
247+
'Port should be > 0 and < 65536. Received 0.');
251248

252249
// Test ERR_TLS_CERT_ALTNAME_INVALID
253250
assert.strictEqual(

0 commit comments

Comments
 (0)