File tree 2 files changed +21
-4
lines changed
2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change
1
+ import type { Stream } from "@libp2p/interface/connection" ;
1
2
import type { PeerId } from "@libp2p/interface/peer-id" ;
2
3
import {
3
4
IEncoder ,
@@ -102,9 +103,24 @@ class LightPush extends BaseProtocol implements ILightPush {
102
103
numPeers : this . NUM_PEERS_PROTOCOL
103
104
} ) ;
104
105
106
+ if ( ! peers . length ) {
107
+ return {
108
+ recipients,
109
+ errors : [ SendError . NO_PEER_AVAILABLE ]
110
+ } ;
111
+ }
112
+
105
113
const promises = peers . map ( async ( peer ) => {
106
114
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
+ }
108
124
109
125
try {
110
126
const res = await pipe (
@@ -126,8 +142,8 @@ class LightPush extends BaseProtocol implements ILightPush {
126
142
recipients . some ( ( recipient ) => recipient . equals ( peer . id ) ) ||
127
143
recipients . push ( peer . id ) ;
128
144
} 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 ;
131
147
}
132
148
} catch ( err ) {
133
149
log ( "Failed to decode push reply" , err ) ;
Original file line number Diff line number Diff line change @@ -63,7 +63,8 @@ export enum SendError {
63
63
ENCODE_FAILED = "Failed to encode" ,
64
64
DECODE_FAILED = "Failed to decode" ,
65
65
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"
67
68
}
68
69
69
70
export interface SendResult {
You can’t perform that action at this time.
0 commit comments