diff --git a/dist/client.js b/dist/client.js index 310ecf1..41d63ec 100644 --- a/dist/client.js +++ b/dist/client.js @@ -142,7 +142,9 @@ class Client { this.client.onready = () => { clearTimeout(connectionTimeout); this._changeState(STATE_NOT_AUTHENTICATED); - this.updateCapability().then(() => resolve(this._capability)); + // reject instead of leaving an unhandled rejection when the socket closes + // while CAPABILITY is pending (e.g. server greets and immediately drops the connection) + this.updateCapability().then(() => resolve(this._capability)).catch(reject); }; this.client.onerror = err => { clearTimeout(connectionTimeout); diff --git a/src/client.js b/src/client.js index 4f0d3d7..d155dac 100644 --- a/src/client.js +++ b/src/client.js @@ -161,8 +161,11 @@ export default class Client { this.client.onready = () => { clearTimeout(connectionTimeout) this._changeState(STATE_NOT_AUTHENTICATED) + // reject instead of leaving an unhandled rejection when the socket closes + // while CAPABILITY is pending (e.g. server greets and immediately drops the connection) this.updateCapability() .then(() => resolve(this._capability)) + .catch(reject) } this.client.onerror = (err) => { diff --git a/src/client.unit.js b/src/client.unit.js index fc1e1d3..f1886fa 100644 --- a/src/client.unit.js +++ b/src/client.unit.js @@ -60,6 +60,18 @@ describe('browserbox unit tests', () => { expect(br._capability[1]).to.equal('capa2') }) }) + + it('should reject when CAPABILITY fails after greeting', () => { + const err = new Error('Socket closed unexpectedly!') + br.client.connect.returns(Promise.resolve()) + br.client.enqueueCommand.callsFake(() => Promise.reject(err)) + setTimeout(() => br.client.onready(), 0) + return br.openConnection().then(() => { + throw new Error('should not resolve') + }, (e) => { + expect(e).to.equal(err) + }) + }) }) describe('#connect', () => {