Skip to content

Commit a31b6e4

Browse files
committed
refactor: Remove nest of try/catch in favor of sequential try/catch
1 parent b696a89 commit a31b6e4

File tree

1 file changed

+26
-26
lines changed
  • packages/core/src/lib/light_push

1 file changed

+26
-26
lines changed

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

+26-26
Original file line numberDiff line numberDiff line change
@@ -111,50 +111,50 @@ class LightPush extends BaseProtocol implements ILightPush {
111111
}
112112

113113
const promises = peers.map(async (peer) => {
114-
let error: SendError | undefined;
115-
116114
let stream: Stream | undefined;
117115
try {
118116
stream = await this.getStream(peer);
119117
} catch (err) {
120118
log(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
121-
error = SendError.REMOTE_PEER_FAULT;
122-
return { recipients, error };
119+
return { recipients, error: SendError.REMOTE_PEER_FAULT };
123120
}
124121

122+
let res: Uint8ArrayList[] | undefined;
125123
try {
126-
const res = await pipe(
124+
res = await pipe(
127125
[query.encode()],
128126
lp.encode,
129127
stream,
130128
lp.decode,
131129
async (source) => await all(source)
132130
);
133-
try {
134-
const bytes = new Uint8ArrayList();
135-
res.forEach((chunk) => {
136-
bytes.append(chunk);
137-
});
138-
139-
const response = PushRpc.decode(bytes).response;
140-
141-
if (response?.isSuccess) {
142-
recipients.some((recipient) => recipient.equals(peer.id)) ||
143-
recipients.push(peer.id);
144-
} else {
145-
log("Remote peer fault: No response in PushRPC");
146-
error = SendError.REMOTE_PEER_FAULT;
147-
}
148-
} catch (err) {
149-
log("Failed to decode push reply", err);
150-
error = SendError.DECODE_FAILED;
151-
}
152131
} catch (err) {
153132
log("Failed to send waku light push request", err);
154-
error = SendError.GENERIC_FAIL;
133+
return { recipients, error: SendError.GENERIC_FAIL };
134+
}
135+
136+
const bytes = new Uint8ArrayList();
137+
res.forEach((chunk) => {
138+
bytes.append(chunk);
139+
});
140+
141+
let response: PushResponse | undefined;
142+
try {
143+
response = PushRpc.decode(bytes).response;
144+
} catch (err) {
145+
log("Failed to decode push reply", err);
146+
return { recipients, error: SendError.DECODE_FAILED };
147+
}
148+
149+
if (response?.isSuccess) {
150+
recipients.some((recipient) => recipient.equals(peer.id)) ||
151+
recipients.push(peer.id);
152+
} else {
153+
log("Remote peer fault: No response in PushRPC");
154+
return { recipients, error: SendError.REMOTE_PEER_FAULT };
155155
}
156156

157-
return { recipients, error };
157+
return { recipients };
158158
});
159159

160160
const results = await Promise.allSettled(promises);

0 commit comments

Comments
 (0)