Skip to content

Commit f2e3a67

Browse files
mhdawsonjasnell
authored andcommitted
dgram: convert to using internal/errors
Covert lib/dgram.js over to using lib/internal/errors.js for generating Errors. See [using-internal-errors.md](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md) for more details. I have not addressed the cases that use errnoException() and exceptionWithHostPort() helper methods as changing these would require fixing the tests across all of the different files that use them. In addition, these helpers already add a `code` to the Error and we'll have to discuss how that interacts with the `code` used by lib/internal/errors.js. I believe we should convert all users of errnoException and exceptionWithHostPort in a PR dedicated to that conversion. PR-URL: #12926 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Ruben Bridgewater <ruben.bridgewater@fintura.de>
1 parent e012f5a commit f2e3a67

11 files changed

+167
-47
lines changed

doc/api/errors.md

+29
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,35 @@ in some cases may accept `func(undefined)` but not `func()`). In most native
703703
Node.js APIs, `func(undefined)` and `func()` are treated identically, and the
704704
[`ERR_INVALID_ARG_TYPE`][] error code may be used instead.
705705

706+
<a id="ERR_SOCKET_ALREADY_BOUND"></a>
707+
### ERR_SOCKET_ALREADY_BOUND
708+
An error using the `'ERR_SOCKET_ALREADY_BOUND'` code is thrown when an attempt
709+
is made to bind a socket that has already been bound.
710+
711+
<a id="ERR_SOCKET_BAD_PORT"></a>
712+
### ERR_SOCKET_BAD_PORT
713+
714+
An error using the `'ERR_SOCKET_BAD_PORT'` code is thrown when an API
715+
function expecting a port > 0 and < 65536 receives an invalid value.
716+
717+
<a id="ERR_SOCKET_BAD_TYPE"></a>
718+
### ERR_SOCKET_BAD_TYPE
719+
720+
An error using the `'ERR_SOCKET_BAD_TYPE'` code is thrown when an API
721+
function expecting a socket type (`udp4` or `udp6`) receives an invalid value.
722+
723+
<a id="ERR_SOCKET_CANNOT_SEND"></a>
724+
### ERR_SOCKET_CANNOT_SEND
725+
726+
An error using the `'ERR_SOCKET_CANNOT_SEND'` code is thrown when data
727+
cannot be sent on a socket.
728+
729+
<a id="ERR_SOCKET_DGRAM_NOT_RUNNING"></a>
730+
### ERR_SOCKET_DGRAM_NOT_RUNNING
731+
732+
An error using the `'ERR_SOCKET_DGRAM_NOT_RUNNING'` code is thrown
733+
when a call is made and the UDP subsystem is not running.
734+
706735
<a id="ERR_STDERR_CLOSE"></a>
707736
### ERR_STDERR_CLOSE
708737

lib/dgram.js

+43-21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const assert = require('assert');
25+
const errors = require('internal/errors');
2526
const Buffer = require('buffer').Buffer;
2627
const util = require('util');
2728
const EventEmitter = require('events');
@@ -78,7 +79,7 @@ function newHandle(type) {
7879
return handle;
7980
}
8081

81-
throw new Error('Bad socket type specified. Valid types are: udp4, udp6');
82+
throw new errors.Error('ERR_SOCKET_BAD_TYPE');
8283
}
8384

8485

@@ -162,7 +163,7 @@ Socket.prototype.bind = function(port_, address_ /*, callback*/) {
162163
this._healthCheck();
163164

164165
if (this._bindState !== BIND_STATE_UNBOUND)
165-
throw new Error('Socket is already bound');
166+
throw new errors.Error('ERR_SOCKET_ALREADY_BOUND');
166167

167168
this._bindState = BIND_STATE_BINDING;
168169

@@ -260,11 +261,21 @@ Socket.prototype.sendto = function(buffer,
260261
port,
261262
address,
262263
callback) {
263-
if (typeof offset !== 'number' || typeof length !== 'number')
264-
throw new Error('Send takes "offset" and "length" as args 2 and 3');
264+
if (typeof offset !== 'number') {
265+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'offset', 'number');
266+
}
267+
268+
if (typeof length !== 'number') {
269+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'length', 'number');
270+
}
265271

266-
if (typeof address !== 'string')
267-
throw new Error(this.type + ' sockets must send to port, address');
272+
if (typeof port !== 'number') {
273+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'port', 'number');
274+
}
275+
276+
if (typeof address !== 'string') {
277+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'address', 'string');
278+
}
268279

269280
this.send(buffer, offset, length, port, address, callback);
270281
};
@@ -274,8 +285,9 @@ function sliceBuffer(buffer, offset, length) {
274285
if (typeof buffer === 'string') {
275286
buffer = Buffer.from(buffer);
276287
} else if (!isUint8Array(buffer)) {
277-
throw new TypeError('First argument must be a Buffer, ' +
278-
'Uint8Array or string');
288+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
289+
'buffer',
290+
['Buffer', 'Uint8Array', 'string']);
279291
}
280292

281293
offset = offset >>> 0;
@@ -323,7 +335,7 @@ function onListenSuccess() {
323335
function onListenError(err) {
324336
this.removeListener('listening', onListenSuccess);
325337
this._queue = undefined;
326-
this.emit('error', new Error('Unable to send data'));
338+
this.emit('error', new errors.Error('ERR_SOCKET_CANNOT_SEND'));
327339
}
328340

329341

@@ -366,18 +378,21 @@ Socket.prototype.send = function(buffer,
366378
if (typeof buffer === 'string') {
367379
list = [ Buffer.from(buffer) ];
368380
} else if (!isUint8Array(buffer)) {
369-
throw new TypeError('First argument must be a Buffer, ' +
370-
'Uint8Array or string');
381+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
382+
'buffer',
383+
['Buffer', 'Uint8Array', 'string']);
371384
} else {
372385
list = [ buffer ];
373386
}
374387
} else if (!(list = fixBufferList(buffer))) {
375-
throw new TypeError('Buffer list arguments must be buffers or strings');
388+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
389+
'buffer list arguments',
390+
['Buffer', 'string']);
376391
}
377392

378393
port = port >>> 0;
379394
if (port === 0 || port > 65535)
380-
throw new RangeError('Port should be > 0 and < 65536');
395+
throw new errors.RangeError('ERR_SOCKET_BAD_PORT');
381396

382397
// Normalize callback so it's either a function or undefined but not anything
383398
// else.
@@ -388,8 +403,9 @@ Socket.prototype.send = function(buffer,
388403
callback = address;
389404
address = undefined;
390405
} else if (address && typeof address !== 'string') {
391-
throw new TypeError('Invalid arguments: address must be a nonempty ' +
392-
'string or falsy');
406+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
407+
'address',
408+
['string', 'falsy']);
393409
}
394410

395411
this._healthCheck();
@@ -510,7 +526,9 @@ Socket.prototype.setBroadcast = function(arg) {
510526

511527
Socket.prototype.setTTL = function(arg) {
512528
if (typeof arg !== 'number') {
513-
throw new TypeError('Argument must be a number');
529+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
530+
'arg',
531+
'number');
514532
}
515533

516534
var err = this._handle.setTTL(arg);
@@ -524,7 +542,9 @@ Socket.prototype.setTTL = function(arg) {
524542

525543
Socket.prototype.setMulticastTTL = function(arg) {
526544
if (typeof arg !== 'number') {
527-
throw new TypeError('Argument must be a number');
545+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
546+
'arg',
547+
'number');
528548
}
529549

530550
var err = this._handle.setMulticastTTL(arg);
@@ -551,7 +571,7 @@ Socket.prototype.addMembership = function(multicastAddress,
551571
this._healthCheck();
552572

553573
if (!multicastAddress) {
554-
throw new Error('multicast address must be specified');
574+
throw new errors.TypeError('ERR_MISSING_ARGS', 'multicastAddress');
555575
}
556576

557577
var err = this._handle.addMembership(multicastAddress, interfaceAddress);
@@ -566,7 +586,7 @@ Socket.prototype.dropMembership = function(multicastAddress,
566586
this._healthCheck();
567587

568588
if (!multicastAddress) {
569-
throw new Error('multicast address must be specified');
589+
throw new errors.TypeError('ERR_MISSING_ARGS', 'multicastAddress');
570590
}
571591

572592
var err = this._handle.dropMembership(multicastAddress, interfaceAddress);
@@ -577,8 +597,10 @@ Socket.prototype.dropMembership = function(multicastAddress,
577597

578598

579599
Socket.prototype._healthCheck = function() {
580-
if (!this._handle)
581-
throw new Error('Not running'); // error message from dgram_legacy.js
600+
if (!this._handle) {
601+
// Error message from dgram_legacy.js.
602+
throw new errors.Error('ERR_SOCKET_DGRAM_NOT_RUNNING');
603+
}
582604
};
583605

584606

lib/internal/errors.js

+6
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`);
142142
E('ERR_UNKNOWN_SIGNAL', (signal) => `Unknown signal: ${signal}`);
143143
E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type');
144144
E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type');
145+
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
146+
E('ERR_SOCKET_BAD_TYPE',
147+
'Bad socket type specified. Valid types are: udp4, udp6');
148+
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
149+
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
150+
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
145151
// Add new errors from here...
146152

147153
function invalidArgType(name, expected, actual) {

test/parallel/test-dgram-bind.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ const socket = dgram.createSocket('udp4');
2929
socket.on('listening', common.mustCall(() => {
3030
assert.throws(() => {
3131
socket.bind();
32-
}, /^Error: Socket is already bound$/);
32+
}, common.expectsError({
33+
code: 'ERR_SOCKET_ALREADY_BOUND',
34+
type: Error,
35+
message: /^Socket is already bound$/
36+
}));
3337

3438
socket.close();
3539
}));

test/parallel/test-dgram-createSocket-type.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const dgram = require('dgram');
55
const invalidTypes = [
@@ -24,7 +24,11 @@ const validTypes = [
2424
invalidTypes.forEach((invalidType) => {
2525
assert.throws(() => {
2626
dgram.createSocket(invalidType);
27-
}, /Bad socket type specified/);
27+
}, common.expectsError({
28+
code: 'ERR_SOCKET_BAD_TYPE',
29+
type: Error,
30+
message: /^Bad socket type specified\. Valid types are: udp4, udp6$/
31+
}));
2832
});
2933

3034
// Error must not be thrown with valid types

test/parallel/test-dgram-implicit-bind-failure.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ socket.on('error', (err) => {
3131
return;
3232
}
3333

34-
if (/^Error: Unable to send data$/.test(err)) {
34+
if (err.code === 'ERR_SOCKET_CANNOT_SEND') {
3535
// On error, the queue should be destroyed and this function should be
3636
// the only listener.
3737
sendFailures++;

test/parallel/test-dgram-membership.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,53 @@ const setup = dgram.createSocket.bind(dgram, {type: 'udp4', reuseAddr: true});
1111
{
1212
const socket = setup();
1313
socket.close(common.mustCall(() => {
14-
assert.throws(() => { socket.addMembership(multicastAddress); },
15-
/^Error: Not running$/);
14+
assert.throws(() => {
15+
socket.addMembership(multicastAddress);
16+
}, common.expectsError({
17+
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
18+
type: Error,
19+
message: /^Not running$/
20+
}));
1621
}));
1722
}
1823

1924
// dropMembership() on closed socket should throw
2025
{
2126
const socket = setup();
2227
socket.close(common.mustCall(() => {
23-
assert.throws(() => { socket.dropMembership(multicastAddress); },
24-
/^Error: Not running$/);
28+
assert.throws(() => {
29+
socket.dropMembership(multicastAddress);
30+
}, common.expectsError({
31+
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
32+
type: Error,
33+
message: /^Not running$/
34+
}));
2535
}));
2636
}
2737

2838
// addMembership() with no argument should throw
2939
{
3040
const socket = setup();
31-
assert.throws(() => { socket.addMembership(); },
32-
/^Error: multicast address must be specified$/);
41+
assert.throws(() => {
42+
socket.addMembership();
43+
}, common.expectsError({
44+
code: 'ERR_MISSING_ARGS',
45+
type: TypeError,
46+
message: /^The "multicastAddress" argument must be specified$/
47+
}));
3348
socket.close();
3449
}
3550

3651
// dropMembership() with no argument should throw
3752
{
3853
const socket = setup();
39-
assert.throws(() => { socket.dropMembership(); },
40-
/^Error: multicast address must be specified$/);
54+
assert.throws(() => {
55+
socket.dropMembership();
56+
}, common.expectsError({
57+
code: 'ERR_MISSING_ARGS',
58+
type: TypeError,
59+
message: /^The "multicastAddress" argument must be specified$/
60+
}));
4161
socket.close();
4262
}
4363

test/parallel/test-dgram-multicast-setTTL.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ socket.on('listening', common.mustCall(() => {
3737

3838
assert.throws(() => {
3939
socket.setMulticastTTL('foo');
40-
}, /^TypeError: Argument must be a number$/);
40+
}, common.expectsError({
41+
code: 'ERR_INVALID_ARG_TYPE',
42+
type: TypeError,
43+
message: /^The "arg" argument must be of type number$/
44+
}));
4145

4246
//close the socket
4347
socket.close();

test/parallel/test-dgram-send-address-types.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ const onMessage = common.mustCall((err, bytes) => {
1010
assert.strictEqual(bytes, buf.length);
1111
}, 6);
1212

13-
const expectedError =
14-
/^TypeError: Invalid arguments: address must be a nonempty string or falsy$/;
13+
const expectedError = { code: 'ERR_INVALID_ARG_TYPE',
14+
type: TypeError,
15+
message:
16+
/^The "address" argument must be one of type string or falsy$/
17+
};
1518

1619
const client = dgram.createSocket('udp4').bind(0, () => {
1720
const port = client.address().port;
@@ -37,17 +40,17 @@ const client = dgram.createSocket('udp4').bind(0, () => {
3740
// invalid address: object
3841
assert.throws(() => {
3942
client.send(buf, port, []);
40-
}, expectedError);
43+
}, common.expectsError(expectedError));
4144

4245
// invalid address: nonzero number
4346
assert.throws(() => {
4447
client.send(buf, port, 1);
45-
}, expectedError);
48+
}, common.expectsError(expectedError));
4649

4750
// invalid address: true
4851
assert.throws(() => {
4952
client.send(buf, port, true);
50-
}, expectedError);
53+
}, common.expectsError(expectedError));
5154
});
5255

5356
client.unref();

0 commit comments

Comments
 (0)