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.
This commit is contained in:
Jacob Heun
2019-08-21 19:08:56 +02:00
committed by GitHub
parent 3eef695bc0
commit 60b0cbc179
2 changed files with 9 additions and 2 deletions

View File

@ -56,10 +56,10 @@ function getPeerInfoRemote (peer, libp2p) {
try {
peerInfo = getPeerInfo(peer, libp2p.peerBook)
} catch (err) {
throw errCode(
return Promise.reject(errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_PEER_TYPE'
)
))
}
// If we don't have an address for the peer, attempt to find it

View File

@ -123,5 +123,12 @@ describe('Get Peer Info', () => {
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
it('should callback with error for invalid non-peer multiaddr (promise)', () => {
return getPeerInfoRemote(undefined)
.then(expect.fail, (err) => {
expect(err.code).to.eql('ERR_INVALID_PEER_TYPE')
})
})
})
})