Skip to content

Commit

Permalink
fix(Client.connect): handle unspecified error in response
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 committed Jul 4, 2019
1 parent 8d6d82c commit 7a34a01
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class Client {
const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
throw errcode(response.ErrorResponse.msg, 'ERR_CONNECT_FAILED')
const errResponse = response.ErrorResponse || response.error || {}
throw errcode(errResponse.msg || 'unspecified', 'ERR_CONNECT_FAILED')
}
}

Expand Down
92 changes: 66 additions & 26 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -257,41 +258,80 @@ describe('daemon client', function () {
}
})

it('should error if receive an error message', async () => {
client = new Client(defaultMultiaddr)
describe('failure', () => {
let stub

await client.attach()
afterEach(() => {
if (stub != null) {
stub.restore()
}
stub = null
})

let identify
try {
identify = await client.identify()
} catch (err) {
expect(err).to.not.exist()
}
it('should error if it receives an error with ErrorResponse property', async () => {
client = new Client(defaultMultiaddr)

const stub = sinon.stub(Response, 'decode').returns({
type: 'ERROR',
ErrorResponse: {
msg: 'mock error'
await client.attach()

let identify
try {
identify = await client.identify()
} catch (err) {
expect(err).to.not.exist()
}

stub = sinon.stub(Response, 'decode').returns({
type: 'ERROR',
ErrorResponse: {
msg: 'mock error'
}
})

// close first client
client.close()

client = new Client(addr2)

await client.attach()

await expect(client.connect(identify.peerId, identify.addrs)).to.be.rejectedWith(
'mock error')
})

// 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 details', async () => {
client = new Client(defaultMultiaddr)
await client.attach()
const identify = await client.identify()
client.close()

await client.attach()
client = new Client(addr2)
await client.attach()

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()
}
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 () => {
Expand Down

0 comments on commit 7a34a01

Please sign in to comment.