@@ -63,6 +63,25 @@ function isPipeName(s) {
63
63
return util . isString ( s ) && toNumber ( s ) === false ;
64
64
}
65
65
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
+ }
66
85
67
86
exports . createServer = function ( ) {
68
87
return new Server ( arguments [ 0 ] , arguments [ 1 ] ) ;
@@ -755,7 +774,7 @@ function afterWrite(status, handle, req, err) {
755
774
}
756
775
757
776
if ( status < 0 ) {
758
- var ex = errnoException ( status , 'write' , err ) ;
777
+ var ex = detailedException ( status , 'write' , req . address , req . port ) ;
759
778
debug ( 'write failure' , ex ) ;
760
779
self . _destroy ( ex , req . cb ) ;
761
780
return ;
@@ -817,28 +836,46 @@ function connect(self, address, port, addressType, localAddress, localPort) {
817
836
err = bind ( localAddress , localPort ) ;
818
837
819
838
if ( err ) {
820
- self . _destroy ( errnoException ( err , 'bind' ) ) ;
839
+ var ex = detailedException ( err , 'bind' , localAddress , localPort ) ;
840
+ self . _destroy ( ex ) ;
821
841
return ;
822
842
}
823
843
}
824
844
825
- var req = { oncomplete : afterConnect } ;
845
+ var req = {
846
+ oncomplete : afterConnect ,
847
+ port : undefined ,
848
+ address : undefined ,
849
+ localAddress : undefined ,
850
+ localPort : undefined
851
+ } ;
826
852
if ( addressType === 6 || addressType === 4 ) {
827
853
port = port | 0 ;
828
854
if ( port <= 0 || port > 65535 )
829
855
throw new RangeError ( 'Port should be > 0 and < 65536' ) ;
830
856
857
+ req . port = port ;
858
+ req . address = address ;
831
859
if ( addressType === 6 ) {
832
860
err = self . _handle . connect6 ( req , address , port ) ;
833
861
} else if ( addressType === 4 ) {
834
862
err = self . _handle . connect ( req , address , port ) ;
835
863
}
836
864
} else {
865
+ req . address = address ;
837
866
err = self . _handle . connect ( req , address , afterConnect ) ;
838
867
}
839
868
840
869
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 ) ;
842
879
}
843
880
}
844
881
@@ -921,6 +958,9 @@ Socket.prototype.connect = function(options, cb) {
921
958
// There are no event listeners registered yet so defer the
922
959
// error event to the next tick.
923
960
process . nextTick ( function ( ) {
961
+ err . host = options . host ;
962
+ err . port = options . port ;
963
+ err . message = err . message + ' ' + options . host + ':' + options . port ;
924
964
self . emit ( 'error' , err ) ;
925
965
self . _destroy ( ) ;
926
966
} ) ;
@@ -988,7 +1028,18 @@ function afterConnect(status, handle, req, readable, writable) {
988
1028
989
1029
} else {
990
1030
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 ) ;
992
1043
}
993
1044
}
994
1045
@@ -1117,7 +1168,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1117
1168
debug ( '_listen2: create a handle' ) ;
1118
1169
var rval = createServerHandle ( address , port , addressType , fd ) ;
1119
1170
if ( util . isNumber ( rval ) ) {
1120
- var error = errnoException ( rval , 'listen' ) ;
1171
+ var error = detailedException ( rval , 'listen' , address , port ) ;
1121
1172
process . nextTick ( function ( ) {
1122
1173
self . emit ( 'error' , error ) ;
1123
1174
} ) ;
@@ -1134,7 +1185,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1134
1185
var err = _listen ( self . _handle , backlog ) ;
1135
1186
1136
1187
if ( err ) {
1137
- var ex = errnoException ( err , 'listen' ) ;
1188
+ var ex = detailedException ( err , 'listen' , address , port ) ;
1138
1189
self . _handle . close ( ) ;
1139
1190
self . _handle = null ;
1140
1191
process . nextTick ( function ( ) {
@@ -1182,8 +1233,10 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
1182
1233
err = uv . UV_EADDRINUSE ;
1183
1234
}
1184
1235
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
+ }
1187
1240
1188
1241
self . _handle = handle ;
1189
1242
self . _listen2 ( address , port , addressType , backlog , fd ) ;
0 commit comments