Skip to content

Commit

Permalink
fix(client.connect): handle unspecified error in response (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 authored and vasco-santos committed Jul 9, 2019
1 parent 80b938d commit 7db681b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 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.error.msg, 'ERR_CONNECT_FAILED')
const errResponse = response.error || {}
throw errcode(errResponse.msg || 'unspecified', 'ERR_CONNECT_FAILED')
}
}

Expand Down
83 changes: 55 additions & 28 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,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 () => {
Expand Down

0 comments on commit 7db681b

Please sign in to comment.