Skip to content

Commit 60b0cbc

Browse files
authored
fix: reject rather than throw in get peer info (#410)
The get peer info util consolidation from #400 exposed an issue with how bad values are being handled. Throwing the error can cause issues when promises are being used. Rejecting resolves this. I added a test case to validate the change.
1 parent 3eef695 commit 60b0cbc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/get-peer-info.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ function getPeerInfoRemote (peer, libp2p) {
5656
try {
5757
peerInfo = getPeerInfo(peer, libp2p.peerBook)
5858
} catch (err) {
59-
throw errCode(
59+
return Promise.reject(errCode(
6060
new Error(`${peer} is not a valid peer type`),
6161
'ERR_INVALID_PEER_TYPE'
62-
)
62+
))
6363
}
6464

6565
// If we don't have an address for the peer, attempt to find it

test/get-peer-info.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,12 @@ describe('Get Peer Info', () => {
123123

124124
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
125125
})
126+
127+
it('should callback with error for invalid non-peer multiaddr (promise)', () => {
128+
return getPeerInfoRemote(undefined)
129+
.then(expect.fail, (err) => {
130+
expect(err.code).to.eql('ERR_INVALID_PEER_TYPE')
131+
})
132+
})
126133
})
127134
})

0 commit comments

Comments
 (0)