Skip to content

Commit c9fd9e2

Browse files
Evan Lucaspiscisaureus
authored andcommitted
dgram: make error messages more informative
PR-URL: #250 Reviewed-By: Bert Belder <bertbelder@gmail.com>
1 parent b1a208c commit c9fd9e2

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

lib/dgram.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var cluster = null;
3838
var dns = null;
3939

4040
var errnoException = util._errnoException;
41+
var exceptionWithHostPort = util._exceptionWithHostPort;
4142

4243
function lookup(address, family, callback) {
4344
if (!dns)
@@ -199,7 +200,8 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
199200
if (cluster.isWorker && !exclusive) {
200201
cluster._getServer(self, ip, port, self.type, -1, function(err, handle) {
201202
if (err) {
202-
self.emit('error', errnoException(err, 'bind'));
203+
var ex = exceptionWithHostPort(err, 'bind', ip, port);
204+
self.emit('error', ex);
203205
self._bindState = BIND_STATE_UNBOUND;
204206
return;
205207
}
@@ -222,7 +224,8 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
222224

223225
var err = self._handle.bind(ip, port || 0, flags);
224226
if (err) {
225-
self.emit('error', errnoException(err, 'bind'));
227+
var ex = exceptionWithHostPort(err, 'bind', ip, port);
228+
self.emit('error', ex);
226229
self._bindState = BIND_STATE_UNBOUND;
227230
// Todo: close?
228231
return;
@@ -325,6 +328,8 @@ Socket.prototype.send = function(buffer,
325328
var req = new SendWrap();
326329
req.buffer = buffer; // Keep reference alive.
327330
req.length = length;
331+
req.address = address;
332+
req.port = port;
328333
if (callback) {
329334
req.callback = callback;
330335
req.oncomplete = afterSend;
@@ -339,7 +344,8 @@ Socket.prototype.send = function(buffer,
339344
if (err && callback) {
340345
// don't emit as error, dgram_legacy.js compatibility
341346
process.nextTick(function() {
342-
callback(errnoException(err, 'send'));
347+
var ex = exceptionWithHostPort(err, 'send', address, port);
348+
callback(ex);
343349
});
344350
}
345351
}
@@ -348,7 +354,10 @@ Socket.prototype.send = function(buffer,
348354

349355

350356
function afterSend(err) {
351-
this.callback(err ? errnoException(err, 'send') : null, this.length);
357+
if (err) {
358+
err = exceptionWithHostPort(err, 'send', this.address, this.port);
359+
}
360+
this.callback(err, this.length);
352361
}
353362

354363

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 dgram = require('dgram');
25+
26+
// IPv4 Test
27+
var socket_ipv4 = dgram.createSocket('udp4');
28+
29+
socket_ipv4.on('listening', assert.fail);
30+
31+
socket_ipv4.on('error', common.mustCall(function(e) {
32+
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1:' + common.PORT);
33+
assert.equal(e.address, '1.1.1.1');
34+
assert.equal(e.port, common.PORT);
35+
assert.equal(e.code, 'EADDRNOTAVAIL');
36+
socket_ipv4.close();
37+
}));
38+
39+
socket_ipv4.bind(common.PORT, '1.1.1.1');
40+
41+
// IPv6 Test
42+
var socket_ipv6 = dgram.createSocket('udp6');
43+
var family_ipv6 = 'IPv6';
44+
45+
socket_ipv6.on('listening', assert.fail);
46+
47+
socket_ipv6.on('error', common.mustCall(function(e) {
48+
assert.equal(e.message, 'bind EADDRNOTAVAIL 111::1:' + common.PORT);
49+
assert.equal(e.address, '111::1');
50+
assert.equal(e.port, common.PORT);
51+
assert.equal(e.code, 'EADDRNOTAVAIL');
52+
socket_ipv6.close();
53+
}));
54+
55+
socket_ipv6.bind(common.PORT, '111::1');

test/parallel/test-dgram-msgsize.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ sock.send(buf, 0, buf.length, 12345, '127.0.0.1', common.mustCall(cb));
3131
function cb(err) {
3232
assert(err instanceof Error);
3333
assert.equal(err.code, 'EMSGSIZE');
34+
assert.equal(err.address, '127.0.0.1');
35+
assert.equal(err.port, 12345);
36+
assert.equal(err.message, 'send EMSGSIZE 127.0.0.1:12345');
3437
sock.close();
3538
}

0 commit comments

Comments
 (0)