Skip to content

Commit b696a89

Browse files
committed
fix: catch stream creation promise rejection for lightPush.send
1 parent a0c96bb commit b696a89

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/core/src/lib/light_push/index.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Stream } from "@libp2p/interface/connection";
12
import type { PeerId } from "@libp2p/interface/peer-id";
23
import {
34
IEncoder,
@@ -102,9 +103,24 @@ class LightPush extends BaseProtocol implements ILightPush {
102103
numPeers: this.NUM_PEERS_PROTOCOL
103104
});
104105

106+
if (!peers.length) {
107+
return {
108+
recipients,
109+
errors: [SendError.NO_PEER_AVAILABLE]
110+
};
111+
}
112+
105113
const promises = peers.map(async (peer) => {
106114
let error: SendError | undefined;
107-
const stream = await this.getStream(peer);
115+
116+
let stream: Stream | undefined;
117+
try {
118+
stream = await this.getStream(peer);
119+
} catch (err) {
120+
log(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
121+
error = SendError.REMOTE_PEER_FAULT;
122+
return { recipients, error };
123+
}
108124

109125
try {
110126
const res = await pipe(
@@ -126,8 +142,8 @@ class LightPush extends BaseProtocol implements ILightPush {
126142
recipients.some((recipient) => recipient.equals(peer.id)) ||
127143
recipients.push(peer.id);
128144
} else {
129-
log("No response in PushRPC");
130-
error = SendError.NO_RPC_RESPONSE;
145+
log("Remote peer fault: No response in PushRPC");
146+
error = SendError.REMOTE_PEER_FAULT;
131147
}
132148
} catch (err) {
133149
log("Failed to decode push reply", err);

packages/interfaces/src/protocols.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export enum SendError {
6363
ENCODE_FAILED = "Failed to encode",
6464
DECODE_FAILED = "Failed to decode",
6565
SIZE_TOO_BIG = "Size is too big",
66-
NO_RPC_RESPONSE = "No RPC response"
66+
NO_PEER_AVAILABLE = "No peer available",
67+
REMOTE_PEER_FAULT = "Remote peer fault"
6768
}
6869

6970
export interface SendResult {

0 commit comments

Comments
 (0)