diff --git a/lib/net.js b/lib/net.js index f1061922ca6150..2da278a32d2f50 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1473,8 +1473,10 @@ Object.defineProperty(Server.prototype, 'listening', { Server.prototype.address = function() { if (this._handle && this._handle.getsockname) { var out = {}; - this._handle.getsockname(out); - // TODO(bnoordhuis) Check err and throw? + var err = this._handle.getsockname(out); + if (err) { + throw errnoException(err, 'address'); + } return out; } else if (this._pipeName) { return this._pipeName; diff --git a/test/parallel/test-socket-address.js b/test/parallel/test-socket-address.js new file mode 100644 index 00000000000000..3e05f89f083223 --- /dev/null +++ b/test/parallel/test-socket-address.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +// This tests checks that if server._handle.getsockname +// returns an error number, an error is thrown. + +const server = net.createServer({}); +server.listen(0, common.mustCall(function() { + server._handle.getsockname = function(out) { + return -1; + }; + assert.throws(() => this.address(), + /^Error: address ([\w|\s-\d])+$/); + server.close(); +}));