22
22
'use strict' ;
23
23
24
24
const assert = require ( 'assert' ) ;
25
+ const errors = require ( 'internal/errors' ) ;
25
26
const Buffer = require ( 'buffer' ) . Buffer ;
26
27
const util = require ( 'util' ) ;
27
28
const EventEmitter = require ( 'events' ) ;
@@ -78,7 +79,7 @@ function newHandle(type) {
78
79
return handle ;
79
80
}
80
81
81
- throw new Error ( 'Bad socket type specified. Valid types are: udp4, udp6 ' ) ;
82
+ throw new errors . Error ( 'ERR_SOCKET_BAD_TYPE ' ) ;
82
83
}
83
84
84
85
@@ -162,7 +163,7 @@ Socket.prototype.bind = function(port_, address_ /*, callback*/) {
162
163
this . _healthCheck ( ) ;
163
164
164
165
if ( this . _bindState !== BIND_STATE_UNBOUND )
165
- throw new Error ( 'Socket is already bound ' ) ;
166
+ throw new errors . Error ( 'ERR_SOCKET_ALREADY_BOUND ' ) ;
166
167
167
168
this . _bindState = BIND_STATE_BINDING ;
168
169
@@ -260,11 +261,21 @@ Socket.prototype.sendto = function(buffer,
260
261
port ,
261
262
address ,
262
263
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
+ }
265
271
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
+ }
268
279
269
280
this . send ( buffer , offset , length , port , address , callback ) ;
270
281
} ;
@@ -274,8 +285,9 @@ function sliceBuffer(buffer, offset, length) {
274
285
if ( typeof buffer === 'string' ) {
275
286
buffer = Buffer . from ( buffer ) ;
276
287
} 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' ] ) ;
279
291
}
280
292
281
293
offset = offset >>> 0 ;
@@ -323,7 +335,7 @@ function onListenSuccess() {
323
335
function onListenError ( err ) {
324
336
this . removeListener ( 'listening' , onListenSuccess ) ;
325
337
this . _queue = undefined ;
326
- this . emit ( 'error' , new Error ( 'Unable to send data ' ) ) ;
338
+ this . emit ( 'error' , new errors . Error ( 'ERR_SOCKET_CANNOT_SEND ' ) ) ;
327
339
}
328
340
329
341
@@ -366,18 +378,21 @@ Socket.prototype.send = function(buffer,
366
378
if ( typeof buffer === 'string' ) {
367
379
list = [ Buffer . from ( buffer ) ] ;
368
380
} 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' ] ) ;
371
384
} else {
372
385
list = [ buffer ] ;
373
386
}
374
387
} 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' ] ) ;
376
391
}
377
392
378
393
port = port >>> 0 ;
379
394
if ( port === 0 || port > 65535 )
380
- throw new RangeError ( 'Port should be > 0 and < 65536 ' ) ;
395
+ throw new errors . RangeError ( 'ERR_SOCKET_BAD_PORT ' ) ;
381
396
382
397
// Normalize callback so it's either a function or undefined but not anything
383
398
// else.
@@ -388,8 +403,9 @@ Socket.prototype.send = function(buffer,
388
403
callback = address ;
389
404
address = undefined ;
390
405
} 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' ] ) ;
393
409
}
394
410
395
411
this . _healthCheck ( ) ;
@@ -510,7 +526,9 @@ Socket.prototype.setBroadcast = function(arg) {
510
526
511
527
Socket . prototype . setTTL = function ( arg ) {
512
528
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' ) ;
514
532
}
515
533
516
534
var err = this . _handle . setTTL ( arg ) ;
@@ -524,7 +542,9 @@ Socket.prototype.setTTL = function(arg) {
524
542
525
543
Socket . prototype . setMulticastTTL = function ( arg ) {
526
544
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' ) ;
528
548
}
529
549
530
550
var err = this . _handle . setMulticastTTL ( arg ) ;
@@ -551,7 +571,7 @@ Socket.prototype.addMembership = function(multicastAddress,
551
571
this . _healthCheck ( ) ;
552
572
553
573
if ( ! multicastAddress ) {
554
- throw new Error ( 'multicast address must be specified ') ;
574
+ throw new errors . TypeError ( 'ERR_MISSING_ARGS' , 'multicastAddress ') ;
555
575
}
556
576
557
577
var err = this . _handle . addMembership ( multicastAddress , interfaceAddress ) ;
@@ -566,7 +586,7 @@ Socket.prototype.dropMembership = function(multicastAddress,
566
586
this . _healthCheck ( ) ;
567
587
568
588
if ( ! multicastAddress ) {
569
- throw new Error ( 'multicast address must be specified ') ;
589
+ throw new errors . TypeError ( 'ERR_MISSING_ARGS' , 'multicastAddress ') ;
570
590
}
571
591
572
592
var err = this . _handle . dropMembership ( multicastAddress , interfaceAddress ) ;
@@ -577,8 +597,10 @@ Socket.prototype.dropMembership = function(multicastAddress,
577
597
578
598
579
599
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
+ }
582
604
} ;
583
605
584
606
0 commit comments