From b696a8957211bf20577f419a207a23ceca03d23f Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 20 Sep 2023 15:56:47 +1000 Subject: [PATCH] fix: catch stream creation promise rejection for `lightPush.send` --- packages/core/src/lib/light_push/index.ts | 22 +++++++++++++++++++--- packages/interfaces/src/protocols.ts | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index d9379c1690..4ecdea0be4 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -1,3 +1,4 @@ +import type { Stream } from "@libp2p/interface/connection"; import type { PeerId } from "@libp2p/interface/peer-id"; import { IEncoder, @@ -102,9 +103,24 @@ class LightPush extends BaseProtocol implements ILightPush { numPeers: this.NUM_PEERS_PROTOCOL }); + if (!peers.length) { + return { + recipients, + errors: [SendError.NO_PEER_AVAILABLE] + }; + } + const promises = peers.map(async (peer) => { let error: SendError | undefined; - const stream = await this.getStream(peer); + + let stream: Stream | undefined; + try { + stream = await this.getStream(peer); + } catch (err) { + log(`Failed to get a stream for remote peer${peer.id.toString()}`, err); + error = SendError.REMOTE_PEER_FAULT; + return { recipients, error }; + } try { const res = await pipe( @@ -126,8 +142,8 @@ class LightPush extends BaseProtocol implements ILightPush { recipients.some((recipient) => recipient.equals(peer.id)) || recipients.push(peer.id); } else { - log("No response in PushRPC"); - error = SendError.NO_RPC_RESPONSE; + log("Remote peer fault: No response in PushRPC"); + error = SendError.REMOTE_PEER_FAULT; } } catch (err) { log("Failed to decode push reply", err); diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index c354b1b740..aa98bef560 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -63,7 +63,8 @@ export enum SendError { ENCODE_FAILED = "Failed to encode", DECODE_FAILED = "Failed to decode", SIZE_TOO_BIG = "Size is too big", - NO_RPC_RESPONSE = "No RPC response" + NO_PEER_AVAILABLE = "No peer available", + REMOTE_PEER_FAULT = "Remote peer fault" } export interface SendResult {