Skip to content

Commit

Permalink
feat: improve circuit err messages (libp2p#250)
Browse files Browse the repository at this point in the history
* feat: improve circuit err handling

* feat: add test to to validate err when circuit not enabled
  • Loading branch information
dryajov authored and daviddias committed May 1, 2018
1 parent 1f6b8f7 commit 63ad87a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,20 @@ function dial (swtch) {

const tKeys = swtch.availableTransports(pi)

const circuitEnabled = Boolean(swtch.transports[Circuit.tag])
let circuitTried = false
nextTransport(tKeys.shift())

function nextTransport (key) {
let transport = key
if (!transport) {
if (circuitTried) {
return cb(new Error(`Circuit already tried!`))
if (!circuitEnabled) {
const msg = `Circuit not enabled and all transports failed to dial peer ${pi.id.toB58String()}!`
return cb(new Error(msg))
}

if (!swtch.transports[Circuit.tag]) {
return cb(new Error(`Circuit not enabled!`))
if (circuitTried) {
return cb(new Error(`No available transports to dial peer ${pi.id.toB58String()}!`))
}

log(`Falling back to dialing over circuit`)
Expand Down
11 changes: 10 additions & 1 deletion test/circuit-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ describe(`circuit`, function () {
], done)
})

it('circuit not enabled and all transports failed', (done) => {
swarmA.dial(swarmC._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Circuit not enabled and all transports failed to dial peer/)
expect(conn).to.not.exist()
done()
})
})

it('.enableCircuitRelay', () => {
swarmA.connection.enableCircuitRelay({ enabled: true })
expect(Object.keys(swarmA.transports).length).to.equal(3)
Expand Down Expand Up @@ -88,7 +97,7 @@ describe(`circuit`, function () {

swarmA.dial(swarmC._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Circuit already tried!/)
expect(err).to.match(/No available transports to dial peer/)
expect(conn).to.not.exist()
expect(dialSpyA.callCount).to.be.eql(1)
done()
Expand Down

0 comments on commit 63ad87a

Please sign in to comment.