From 7be46169a4970a1be70b12e3184bd210edc587e6 Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Wed, 3 Jul 2019 10:07:29 +0200 Subject: [PATCH] fix(client.connect): handle unspecified error in response --- package.json | 1 + src/index.js | 3 +- test/index.spec.js | 83 ++++++++++++++++++++++++++++++---------------- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 1e05208..2e43507 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "aegir": "^18.2.1", "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "chai-bytes": "~0.1.2", "dirty-chai": "^2.0.1", "mocha": "^6.1.2", diff --git a/src/index.js b/src/index.js index b9ec8af..4cee075 100644 --- a/src/index.js +++ b/src/index.js @@ -164,7 +164,8 @@ class Client { const response = Response.decode(message) if (response.type !== Response.Type.OK) { - throw errcode(response.error.msg, 'ERR_CONNECT_FAILED') + const errResponse = response.error || {} + throw errcode(errResponse.msg || 'unspecified', 'ERR_CONNECT_FAILED') } } diff --git a/test/index.spec.js b/test/index.spec.js index b2ffa5e..85434b6 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -5,6 +5,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +chai.use(require('chai-as-promised')) const sinon = require('sinon') const { createDaemon } = require('libp2p-daemon/src/daemon') @@ -257,41 +258,67 @@ describe('daemon client', function () { } }) - it('should error if receive an error message', async () => { - client = new Client(defaultMultiaddr) - - await client.attach() - - let identify - try { - identify = await client.identify() - } catch (err) { - expect(err).to.not.exist() - } + describe('failure', () => { + let stub - const stub = sinon.stub(Response, 'decode').returns({ - type: 'ERROR', - error: { - msg: 'mock error' + afterEach(() => { + if (stub != null) { + stub.restore() } + stub = null }) - // close first client - client.close() + it('should error if it receives an error with error property', async () => { + client = new Client(defaultMultiaddr) + await client.attach() + const identify = await client.identify() + client.close() + + client = new Client(addr2) + await client.attach() + + stub = sinon.stub(Response, 'decode').returns({ + type: 'ERROR', + error: { + msg: 'mock error' + } + }) + await expect(client.connect(identify.peerId, identify.addrs)).to.be.rejectedWith( + 'mock error') + }) - client = new Client(addr2) + it('should error if it receives an error without message', async () => { + client = new Client(defaultMultiaddr) + await client.attach() + const identify = await client.identify() + client.close() + + client = new Client(addr2) + await client.attach() + + stub = sinon.stub(Response, 'decode').returns({ + type: 'ERROR', + error: {} + }) + await expect(client.connect(identify.peerId, identify.addrs)).to.be.rejectedWith( + 'unspecified') + }) - await client.attach() + it('should error if it receives an error without details', async () => { + client = new Client(defaultMultiaddr) + await client.attach() + const identify = await client.identify() + client.close() - try { - await client.connect(identify.peerId, identify.addrs) - expect.fail('should have thrown') - } catch (err) { - expect(err).to.exist() - expect(err.toString()).to.equal('Error: mock error') - } finally { - stub.restore() - } + client = new Client(addr2) + await client.attach() + + stub = sinon.stub(Response, 'decode').returns({ + type: 'ERROR' + }) + await expect(client.connect(identify.peerId, identify.addrs)).to.be.rejectedWith( + 'unspecified') + }) }) it('should error if receive an invalid peerid', async () => {