@@ -63,6 +63,25 @@ function isPipeName(s) {
6363 return util . isString ( s ) && toNumber ( s ) === false ;
6464}
6565
66+ // format exceptions
67+ function detailedException ( err , syscall , address , port , additional ) {
68+ var details ;
69+ if ( port && port > 0 ) {
70+ details = address + ':' + port ;
71+ } else {
72+ details = address ;
73+ }
74+
75+ if ( additional ) {
76+ details += ' - Local (' + additional + ')' ;
77+ }
78+ var ex = errnoException ( err , syscall , details ) ;
79+ ex . address = address ;
80+ if ( port ) {
81+ ex . port = port ;
82+ }
83+ return ex ;
84+ }
6685
6786exports . createServer = function ( ) {
6887 return new Server ( arguments [ 0 ] , arguments [ 1 ] ) ;
@@ -755,7 +774,7 @@ function afterWrite(status, handle, req, err) {
755774 }
756775
757776 if ( status < 0 ) {
758- var ex = errnoException ( status , 'write' , err ) ;
777+ var ex = detailedException ( status , 'write' , req . address , req . port ) ;
759778 debug ( 'write failure' , ex ) ;
760779 self . _destroy ( ex , req . cb ) ;
761780 return ;
@@ -817,28 +836,46 @@ function connect(self, address, port, addressType, localAddress, localPort) {
817836 err = bind ( localAddress , localPort ) ;
818837
819838 if ( err ) {
820- self . _destroy ( errnoException ( err , 'bind' ) ) ;
839+ var ex = detailedException ( err , 'bind' , localAddress , localPort ) ;
840+ self . _destroy ( ex ) ;
821841 return ;
822842 }
823843 }
824844
825- var req = { oncomplete : afterConnect } ;
845+ var req = {
846+ oncomplete : afterConnect ,
847+ port : undefined ,
848+ address : undefined ,
849+ localAddress : undefined ,
850+ localPort : undefined
851+ } ;
826852 if ( addressType === 6 || addressType === 4 ) {
827853 port = port | 0 ;
828854 if ( port <= 0 || port > 65535 )
829855 throw new RangeError ( 'Port should be > 0 and < 65536' ) ;
830856
857+ req . port = port ;
858+ req . address = address ;
831859 if ( addressType === 6 ) {
832860 err = self . _handle . connect6 ( req , address , port ) ;
833861 } else if ( addressType === 4 ) {
834862 err = self . _handle . connect ( req , address , port ) ;
835863 }
836864 } else {
865+ req . address = address ;
837866 err = self . _handle . connect ( req , address , afterConnect ) ;
838867 }
839868
840869 if ( err ) {
841- self . _destroy ( errnoException ( err , 'connect' ) ) ;
870+ self . _getsockname ( ) ;
871+ var details ;
872+ if ( self . _sockname ) {
873+ ex . localAddress = self . _sockname . address ;
874+ ex . localPort = self . _sockname . port ;
875+ details = ex . localAddress + ':' + ex . localPort ;
876+ }
877+ var ex = detailedException ( err , 'connect' , address , port , details ) ;
878+ self . _destroy ( ex ) ;
842879 }
843880}
844881
@@ -921,6 +958,9 @@ Socket.prototype.connect = function(options, cb) {
921958 // There are no event listeners registered yet so defer the
922959 // error event to the next tick.
923960 process . nextTick ( function ( ) {
961+ err . host = options . host ;
962+ err . port = options . port ;
963+ err . message = err . message + ' ' + options . host + ':' + options . port ;
924964 self . emit ( 'error' , err ) ;
925965 self . _destroy ( ) ;
926966 } ) ;
@@ -988,7 +1028,18 @@ function afterConnect(status, handle, req, readable, writable) {
9881028
9891029 } else {
9901030 self . _connecting = false ;
991- self . _destroy ( errnoException ( status , 'connect' ) ) ;
1031+ var details ;
1032+ if ( req . localAddress && req . localPort ) {
1033+ ex . localAddress = req . localAddress ;
1034+ ex . localPort = req . localPort ;
1035+ details = ex . localAddress + ':' + ex . localPort ;
1036+ }
1037+ var ex = detailedException ( status ,
1038+ 'connect' ,
1039+ req . address ,
1040+ req . port ,
1041+ details ) ;
1042+ self . _destroy ( ex ) ;
9921043 }
9931044}
9941045
@@ -1117,7 +1168,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11171168 debug ( '_listen2: create a handle' ) ;
11181169 var rval = createServerHandle ( address , port , addressType , fd ) ;
11191170 if ( util . isNumber ( rval ) ) {
1120- var error = errnoException ( rval , 'listen' ) ;
1171+ var error = detailedException ( rval , 'listen' , address , port ) ;
11211172 process . nextTick ( function ( ) {
11221173 self . emit ( 'error' , error ) ;
11231174 } ) ;
@@ -1134,7 +1185,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11341185 var err = _listen ( self . _handle , backlog ) ;
11351186
11361187 if ( err ) {
1137- var ex = errnoException ( err , 'listen' ) ;
1188+ var ex = detailedException ( err , 'listen' , address , port ) ;
11381189 self . _handle . close ( ) ;
11391190 self . _handle = null ;
11401191 process . nextTick ( function ( ) {
@@ -1182,8 +1233,10 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
11821233 err = uv . UV_EADDRINUSE ;
11831234 }
11841235
1185- if ( err )
1186- return self . emit ( 'error' , errnoException ( err , 'bind' ) ) ;
1236+ if ( err ) {
1237+ var ex = detailedException ( err , 'bind' , address , port ) ;
1238+ return self . emit ( 'error' , ex ) ;
1239+ }
11871240
11881241 self . _handle = handle ;
11891242 self . _listen2 ( address , port , addressType , backlog , fd ) ;
0 commit comments