Browse Source

dgram: make error messages more informative

PR-URL: https://github.com/iojs/io.js/pull/250
Reviewed-By: Bert Belder <bertbelder@gmail.com>
v1.8.0-commit
Evan Lucas 10 years ago
committed by Evan Lucas
parent
commit
c9fd9e2162
  1. 17
      lib/dgram.js
  2. 55
      test/parallel/test-dgram-error-message-address.js
  3. 3
      test/parallel/test-dgram-msgsize.js

17
lib/dgram.js

@ -38,6 +38,7 @@ var cluster = null;
var dns = null;
var errnoException = util._errnoException;
var exceptionWithHostPort = util._exceptionWithHostPort;
function lookup(address, family, callback) {
if (!dns)
@ -199,7 +200,8 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
if (cluster.isWorker && !exclusive) {
cluster._getServer(self, ip, port, self.type, -1, function(err, handle) {
if (err) {
self.emit('error', errnoException(err, 'bind'));
var ex = exceptionWithHostPort(err, 'bind', ip, port);
self.emit('error', ex);
self._bindState = BIND_STATE_UNBOUND;
return;
}
@ -222,7 +224,8 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
var err = self._handle.bind(ip, port || 0, flags);
if (err) {
self.emit('error', errnoException(err, 'bind'));
var ex = exceptionWithHostPort(err, 'bind', ip, port);
self.emit('error', ex);
self._bindState = BIND_STATE_UNBOUND;
// Todo: close?
return;
@ -325,6 +328,8 @@ Socket.prototype.send = function(buffer,
var req = new SendWrap();
req.buffer = buffer; // Keep reference alive.
req.length = length;
req.address = address;
req.port = port;
if (callback) {
req.callback = callback;
req.oncomplete = afterSend;
@ -339,7 +344,8 @@ Socket.prototype.send = function(buffer,
if (err && callback) {
// don't emit as error, dgram_legacy.js compatibility
process.nextTick(function() {
callback(errnoException(err, 'send'));
var ex = exceptionWithHostPort(err, 'send', address, port);
callback(ex);
});
}
}
@ -348,7 +354,10 @@ Socket.prototype.send = function(buffer,
function afterSend(err) {
this.callback(err ? errnoException(err, 'send') : null, this.length);
if (err) {
err = exceptionWithHostPort(err, 'send', this.address, this.port);
}
this.callback(err, this.length);
}

55
test/parallel/test-dgram-error-message-address.js

@ -0,0 +1,55 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
var dgram = require('dgram');
// IPv4 Test
var socket_ipv4 = dgram.createSocket('udp4');
socket_ipv4.on('listening', assert.fail);
socket_ipv4.on('error', common.mustCall(function(e) {
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1:' + common.PORT);
assert.equal(e.address, '1.1.1.1');
assert.equal(e.port, common.PORT);
assert.equal(e.code, 'EADDRNOTAVAIL');
socket_ipv4.close();
}));
socket_ipv4.bind(common.PORT, '1.1.1.1');
// IPv6 Test
var socket_ipv6 = dgram.createSocket('udp6');
var family_ipv6 = 'IPv6';
socket_ipv6.on('listening', assert.fail);
socket_ipv6.on('error', common.mustCall(function(e) {
assert.equal(e.message, 'bind EADDRNOTAVAIL 111::1:' + common.PORT);
assert.equal(e.address, '111::1');
assert.equal(e.port, common.PORT);
assert.equal(e.code, 'EADDRNOTAVAIL');
socket_ipv6.close();
}));
socket_ipv6.bind(common.PORT, '111::1');

3
test/parallel/test-dgram-msgsize.js

@ -31,5 +31,8 @@ sock.send(buf, 0, buf.length, 12345, '127.0.0.1', common.mustCall(cb));
function cb(err) {
assert(err instanceof Error);
assert.equal(err.code, 'EMSGSIZE');
assert.equal(err.address, '127.0.0.1');
assert.equal(err.port, 12345);
assert.equal(err.message, 'send EMSGSIZE 127.0.0.1:12345');
sock.close();
}

Loading…
Cancel
Save