Skip to content

Commit

Permalink
Merge pull request #13 from robertkiel/fix-uncaught-promise-rejection
Browse files Browse the repository at this point in the history
Fix uncaught promise rejection
  • Loading branch information
Robert Kiel committed Dec 3, 2021
2 parents 638f408 + eb72430 commit bc60449
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,16 @@ class Libp2p extends EventEmitter {
* @param {PeerId|Multiaddr|string} peer - the peer to close connections to
* @returns {Promise<void>}
*/
hangUp (peer) {
const peerInfo = getPeerInfo(peer, this.peerStore)
async hangUp (peer) {
const { id } = getPeer(peer)

let connections = this.registrar.connections.get(peerInfo.id.toB58String())
const connections = this.connectionManager.connections.get(id.toB58String())

if (!connections) {
return Promise.resolve()
return
}

return Promise.all(
await Promise.all(
connections.map(connection => {
return connection.close()
})
Expand Down
8 changes: 7 additions & 1 deletion src/peer-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ class PeerRouting {

const output = await pipe(
merge(
...this._routers.map(router => [router.findPeer(id, options)])
...this._routers.map(router => (async function* () {
try {
yield [await router.findPeer(id, options)];
} catch (err) {
yield;
}
})())
),
(source) => filter(source, Boolean),
// @ts-ignore findPeer resolves a Promise
Expand Down
8 changes: 8 additions & 0 deletions test/peer-routing/peer-routing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ describe('peer-routing', () => {
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_FIND_SELF')
})

it('should handle error', async () => {
const unknownPeers = await peerUtils.createPeerId({ number: 1, fixture: false})

await expect(nodes[0].peerRouting.findPeer(unknownPeers[0]))
.to.eventually.be.rejected()
.and.to.have.property('code', 'NOT_FOUND')
})
})

describe('via delegate router', () => {
Expand Down

0 comments on commit bc60449

Please sign in to comment.