Skip to content

Commit 2c9a17c

Browse files
committed
net: give better error messages
Add address and/or port to errors where applicable for better reporting. See nodejs#7005 Use util.format instead of string concatenation Also move some logic into util._errnoException. Passing an object as the fourth parameter to util._errnoException will now extend the returned error.
1 parent 912b5e0 commit 2c9a17c

7 files changed

+273
-9
lines changed

lib/net.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,12 @@ function afterWrite(status, handle, req, err) {
742742
}
743743

744744
if (status < 0) {
745-
var ex = errnoException(status, 'write', err);
745+
err = util.format('%s %s:%s', err || '', req.address, req.port);
746+
var additions = {
747+
address: req.address
748+
};
749+
if (req.port) additions.port = req.port;
750+
var ex = errnoException(status, 'write', err, additions);
746751
debug('write failure', ex);
747752
self._destroy(ex, req.cb);
748753
return;
@@ -804,7 +809,13 @@ function connect(self, address, port, addressType, localAddress, localPort) {
804809
err = bind(localAddress, localPort);
805810

806811
if (err) {
807-
self._destroy(errnoException(err, 'bind'));
812+
var details = util.format('%s:%s', address, port || '');
813+
var additions = {
814+
address: address
815+
};
816+
if (port) additions.port = port;
817+
var ex = errnoException(err, 'bind', details, additions);
818+
self._destroy(ex);
808819
return;
809820
}
810821
}
@@ -815,17 +826,26 @@ function connect(self, address, port, addressType, localAddress, localPort) {
815826
if (port <= 0 || port > 65535)
816827
throw new RangeError('Port should be > 0 and < 65536');
817828

829+
req.port = port;
830+
req.address = address;
818831
if (addressType === 6) {
819832
err = self._handle.connect6(req, address, port);
820833
} else if (addressType === 4) {
821834
err = self._handle.connect(req, address, port);
822835
}
823836
} else {
837+
req.address = address;
824838
err = self._handle.connect(req, address, afterConnect);
825839
}
826840

827841
if (err) {
828-
self._destroy(errnoException(err, 'connect'));
842+
var details = util.format('%s:%s', address, port || '');
843+
var additions = {
844+
address: address
845+
};
846+
if (port) additions.port = port;
847+
var ex = errnoException(err, 'connect', details, additions);
848+
self._destroy(ex);
829849
}
830850
}
831851

@@ -908,6 +928,7 @@ Socket.prototype.connect = function(options, cb) {
908928
// There are no event listeners registered yet so defer the
909929
// error event to the next tick.
910930
process.nextTick(function() {
931+
if (options.port) err.port = options.port;
911932
self.emit('error', err);
912933
self._destroy();
913934
});
@@ -975,7 +996,13 @@ function afterConnect(status, handle, req, readable, writable) {
975996

976997
} else {
977998
self._connecting = false;
978-
self._destroy(errnoException(status, 'connect'));
999+
var details = util.format('%s:%s', req.address, req.port || '');
1000+
var additions = {
1001+
address: req.address
1002+
};
1003+
if (req.port) additions.port = req.port;
1004+
var ex = errnoException(status, 'connect', details, additions);
1005+
self._destroy(ex);
9791006
}
9801007
}
9811008

@@ -1103,7 +1130,12 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11031130
debug('_listen2: create a handle');
11041131
var rval = createServerHandle(address, port, addressType, fd);
11051132
if (util.isNumber(rval)) {
1106-
var error = errnoException(rval, 'listen');
1133+
var details = util.format('%s:%s', address, port > 0 ? port : '');
1134+
var additions = {
1135+
address: address
1136+
};
1137+
if (port > 0) additions.port = port;
1138+
var error = errnoException(rval, 'listen', details, additions);
11071139
process.nextTick(function() {
11081140
self.emit('error', error);
11091141
});
@@ -1120,7 +1152,12 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11201152
var err = _listen(self._handle, backlog);
11211153

11221154
if (err) {
1123-
var ex = errnoException(err, 'listen');
1155+
var details = util.format('%s:%s', address, port > 0 ? port : '');
1156+
var additions = {
1157+
address: address
1158+
};
1159+
if (port > 0) additions.port = port;
1160+
var ex = errnoException(err, 'listen', details, additions);
11241161
self._handle.close();
11251162
self._handle = null;
11261163
process.nextTick(function() {
@@ -1166,8 +1203,16 @@ function listen(self, address, port, addressType, backlog, fd) {
11661203
err = uv.UV_EADDRINUSE;
11671204
}
11681205

1169-
if (err)
1170-
return self.emit('error', errnoException(err, 'bind'));
1206+
if (err) {
1207+
var details = util.format('%s:%s', address, port > 0 ? port : '');
1208+
var additions = {
1209+
address: address
1210+
};
1211+
if (port > 0) additions.port = port;
1212+
var ex = errnoException(err, 'bind', details, additions);
1213+
return self.emit('error', ex);
1214+
}
1215+
11711216

11721217
self._handle = handle;
11731218
self._listen2(address, port, addressType, backlog, fd);

lib/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
733733

734734

735735
var uv;
736-
exports._errnoException = function(err, syscall, original) {
736+
exports._errnoException = function(err, syscall, original, additions) {
737737
if (isUndefined(uv)) uv = process.binding('uv');
738738
var errname = uv.errname(err);
739739
var message = syscall + ' ' + errname;
@@ -743,5 +743,7 @@ exports._errnoException = function(err, syscall, original) {
743743
e.code = errname;
744744
e.errno = errname;
745745
e.syscall = syscall;
746+
if (additions)
747+
e = exports._extend(e, additions);
746748
return e;
747749
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
var gotError = false;
26+
var fp = '/blah/fadfa';
27+
var server = net.createServer(function(socket) {
28+
});
29+
server.listen(fp, function() {
30+
assert(false);
31+
});
32+
server.on('error', function(e) {
33+
console.error('error', e);
34+
gotError = true;
35+
});
36+
37+
process.on('exit', function() {
38+
assert(gotError);
39+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
var gotError = false;
26+
27+
var server = net.createServer(function(socket) {
28+
});
29+
server.listen(1, '1.1.1.1', function() { // EACCESS or EADDRNOTAVAIL
30+
assert(false);
31+
});
32+
server.on('error', function(e) {
33+
console.error('error', e);
34+
gotError = true;
35+
assert.equal('1.1.1.1', e.address);
36+
assert.equal(1, e.port);
37+
});
38+
39+
process.on('exit', function() {
40+
assert(gotError);
41+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
24+
var common = require('../common');
25+
var net = require('net');
26+
var assert = require('assert');
27+
var fp = '/tmp/fadagagsdfgsdf';
28+
var c = net.connect(fp);
29+
30+
c.on('connect', function() {
31+
console.error('connected?!');
32+
assert.ok(false);
33+
});
34+
35+
var gotError = false;
36+
c.on('error', function(e) {
37+
console.error('couldn\'t connect.', e);
38+
gotError = true;
39+
assert.equal('ENOENT', e.code);
40+
assert.equal(fp, e.address);
41+
});
42+
43+
process.on('exit', function() {
44+
assert.ok(gotError);
45+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
24+
var common = require('../common');
25+
var net = require('net');
26+
var assert = require('assert');
27+
28+
var c = net.createConnection(common.PORT, 'blah');
29+
30+
c.on('connect', function() {
31+
console.error('connected?!');
32+
assert.ok(false);
33+
});
34+
35+
var gotError = false;
36+
c.on('error', function(e) {
37+
console.error('couldn\'t connect.', e);
38+
gotError = true;
39+
assert.equal('ENOTFOUND', e.code);
40+
assert.equal(common.PORT, e.port);
41+
assert.equal('blah', e.hostname);
42+
});
43+
44+
process.on('exit', function() {
45+
assert.ok(gotError);
46+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
24+
var common = require('../common');
25+
var net = require('net');
26+
var assert = require('assert');
27+
28+
var c = net.createConnection(common.PORT);
29+
30+
c.on('connect', function() {
31+
console.error('connected?!');
32+
assert.ok(false);
33+
});
34+
35+
var gotError = false;
36+
c.on('error', function(e) {
37+
console.error('couldn\'t connect.', e);
38+
gotError = true;
39+
assert.equal('ECONNREFUSED', e.code);
40+
assert.equal(common.PORT, e.port);
41+
assert.equal('127.0.0.1', e.address);
42+
});
43+
44+
process.on('exit', function() {
45+
assert.ok(gotError);
46+
});

0 commit comments

Comments
 (0)