Skip to content

Commit 7edc108

Browse files
author
Eran Hammer
committed
Merge pull request hapijs#2570 from arb/error
Added "error" event listener
2 parents 0c82673 + f529f54 commit 7edc108

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/connection.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ internals.Connection.prototype._init = function () {
138138

139139
internals.Connection.prototype._start = function (callback) {
140140

141+
var self = this;
142+
141143
if (this._started) {
142144
return process.nextTick(callback);
143145
}
@@ -149,12 +151,26 @@ internals.Connection.prototype._start = function (callback) {
149151
return process.nextTick(callback);
150152
}
151153

154+
var onError = function (err) {
155+
156+
self._started = false;
157+
return callback(err);
158+
};
159+
160+
this.listener.once('error', onError);
161+
162+
var finalize = function () {
163+
164+
self.listener.removeListener('error', onError);
165+
callback();
166+
};
167+
152168
if (this.type !== 'tcp') {
153-
this.listener.listen(this.settings.port, callback);
169+
this.listener.listen(this.settings.port, finalize);
154170
}
155171
else {
156172
var address = this.settings.address || this.settings.host || '0.0.0.0';
157-
this.listener.listen(this.settings.port, address, callback);
173+
this.listener.listen(this.settings.port, address, finalize);
158174
}
159175
};
160176

test/connection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,24 @@ describe('Connection', function () {
399399
});
400400
});
401401
});
402+
403+
it('will return an error if the port is aleady in use', function (done) {
404+
405+
var server = new Hapi.Server();
406+
server.connection();
407+
408+
server.start(function (err) {
409+
410+
expect(err).to.not.exist();
411+
server.connection({ port: server.info.port });
412+
server.start(function (err) {
413+
414+
expect(err).to.exist();
415+
expect(err.message).to.match(/EADDRINUSE/);
416+
done();
417+
});
418+
});
419+
});
402420
});
403421

404422
describe('_stop()', function () {

0 commit comments

Comments
 (0)