fix: catch stream creation promise rejection for `lightPush.send`

This commit is contained in:
fryorcraken.eth 2023-09-20 15:56:47 +10:00
parent a0c96bb3f3
commit b696a89572
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 21 additions and 4 deletions

View File

@ -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);

View File

@ -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 {