Skip to content

Commit 63ad87a

Browse files
dryajovdaviddias
authored andcommitted
feat: improve circuit err messages (libp2p#250)
* feat: improve circuit err handling * feat: add test to to validate err when circuit not enabled
1 parent 1f6b8f7 commit 63ad87a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/dial.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,20 @@ function dial (swtch) {
8989

9090
const tKeys = swtch.availableTransports(pi)
9191

92+
const circuitEnabled = Boolean(swtch.transports[Circuit.tag])
9293
let circuitTried = false
9394
nextTransport(tKeys.shift())
9495

9596
function nextTransport (key) {
9697
let transport = key
9798
if (!transport) {
98-
if (circuitTried) {
99-
return cb(new Error(`Circuit already tried!`))
99+
if (!circuitEnabled) {
100+
const msg = `Circuit not enabled and all transports failed to dial peer ${pi.id.toB58String()}!`
101+
return cb(new Error(msg))
100102
}
101103

102-
if (!swtch.transports[Circuit.tag]) {
103-
return cb(new Error(`Circuit not enabled!`))
104+
if (circuitTried) {
105+
return cb(new Error(`No available transports to dial peer ${pi.id.toB58String()}!`))
104106
}
105107

106108
log(`Falling back to dialing over circuit`)

test/circuit-relay.node.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ describe(`circuit`, function () {
5454
], done)
5555
})
5656

57+
it('circuit not enabled and all transports failed', (done) => {
58+
swarmA.dial(swarmC._peerInfo, (err, conn) => {
59+
expect(err).to.exist()
60+
expect(err).to.match(/Circuit not enabled and all transports failed to dial peer/)
61+
expect(conn).to.not.exist()
62+
done()
63+
})
64+
})
65+
5766
it('.enableCircuitRelay', () => {
5867
swarmA.connection.enableCircuitRelay({ enabled: true })
5968
expect(Object.keys(swarmA.transports).length).to.equal(3)
@@ -88,7 +97,7 @@ describe(`circuit`, function () {
8897

8998
swarmA.dial(swarmC._peerInfo, (err, conn) => {
9099
expect(err).to.exist()
91-
expect(err).to.match(/Circuit already tried!/)
100+
expect(err).to.match(/No available transports to dial peer/)
92101
expect(conn).to.not.exist()
93102
expect(dialSpyA.callCount).to.be.eql(1)
94103
done()

0 commit comments

Comments
 (0)