2222'use strict' ;
2323
2424const assert = require ( 'assert' ) ;
25+ const errors = require ( 'internal/errors' ) ;
2526const Buffer = require ( 'buffer' ) . Buffer ;
2627const util = require ( 'util' ) ;
2728const 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() {
323335function 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
511527Socket . 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
525543Socket . 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
579599Socket . 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
0 commit comments