diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index 341c68b0fc..336311f3e0 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -268,15 +268,12 @@ class Filter implements IFilter { } private async getPeer(peerId?: PeerId): Promise { - const res = await selectPeerForProtocol( + const { peer } = await selectPeerForProtocol( this.peerStore, [this.multicodec], peerId ); - if (!res) { - throw new Error(`Failed to select peer for ${this.multicodec}`); - } - return res.peer; + return peer; } async peers(): Promise { diff --git a/packages/peer-exchange/src/waku_peer_exchange.ts b/packages/peer-exchange/src/waku_peer_exchange.ts index ec66bae934..79c7180c1c 100644 --- a/packages/peer-exchange/src/waku_peer_exchange.ts +++ b/packages/peer-exchange/src/waku_peer_exchange.ts @@ -119,15 +119,12 @@ export class WakuPeerExchange implements IPeerExchange { * @returns A peer to query */ private async getPeer(peerId?: PeerId): Promise { - const res = await selectPeerForProtocol( + const { peer } = await selectPeerForProtocol( this.peerStore, [PeerExchangeCodec], peerId ); - if (!res) { - throw new Error(`Failed to select peer for ${PeerExchangeCodec}`); - } - return res.peer; + return peer; } /** diff --git a/packages/utils/src/libp2p/index.ts b/packages/utils/src/libp2p/index.ts index f760a16953..13536c6ba5 100644 --- a/packages/utils/src/libp2p/index.ts +++ b/packages/utils/src/libp2p/index.ts @@ -42,22 +42,22 @@ export async function selectPeerForProtocol( peerStore: PeerStore, protocols: string[], peerId?: PeerId -): Promise<{ peer: Peer; protocol: string } | undefined> { +): Promise<{ peer: Peer; protocol: string }> { let peer; if (peerId) { peer = await peerStore.get(peerId); if (!peer) { - log( + throw new Error( `Failed to retrieve connection details for provided peer in peer store: ${peerId.toString()}` ); - return; } } else { const peers = await getPeersForProtocol(peerStore, protocols); peer = selectRandomPeer(peers); if (!peer) { - log("Failed to find known peer that registers protocols", protocols); - return; + throw new Error( + `Failed to find known peer that registers protocols: ${protocols}` + ); } } @@ -70,11 +70,9 @@ export async function selectPeerForProtocol( } log(`Using codec ${protocol}`); if (!protocol) { - log( - `Peer does not register required protocols: ${peer.id.toString()}`, - protocols + throw new Error( + `Peer does not register required protocols (${peer.id.toString()}): ${protocols}` ); - return; } return { peer, protocol };