Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client.connect: Handle unspecified error in response #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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