refactor: Remove nest of try/catch in favor of sequential try/catch

This commit is contained in:
fryorcraken.eth 2023-09-20 16:02:52 +10:00
parent b696a89572
commit a31b6e472e
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 26 additions and 26 deletions

View File

@ -111,50 +111,50 @@ class LightPush extends BaseProtocol implements ILightPush {
} }
const promises = peers.map(async (peer) => { const promises = peers.map(async (peer) => {
let error: SendError | undefined;
let stream: Stream | undefined; let stream: Stream | undefined;
try { try {
stream = await this.getStream(peer); stream = await this.getStream(peer);
} catch (err) { } catch (err) {
log(`Failed to get a stream for remote peer${peer.id.toString()}`, err); log(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
error = SendError.REMOTE_PEER_FAULT; return { recipients, error: SendError.REMOTE_PEER_FAULT };
return { recipients, error };
} }
let res: Uint8ArrayList[] | undefined;
try { try {
const res = await pipe( res = await pipe(
[query.encode()], [query.encode()],
lp.encode, lp.encode,
stream, stream,
lp.decode, lp.decode,
async (source) => await all(source) async (source) => await all(source)
); );
try { } catch (err) {
log("Failed to send waku light push request", err);
return { recipients, error: SendError.GENERIC_FAIL };
}
const bytes = new Uint8ArrayList(); const bytes = new Uint8ArrayList();
res.forEach((chunk) => { res.forEach((chunk) => {
bytes.append(chunk); bytes.append(chunk);
}); });
const response = PushRpc.decode(bytes).response; let response: PushResponse | undefined;
try {
response = PushRpc.decode(bytes).response;
} catch (err) {
log("Failed to decode push reply", err);
return { recipients, error: SendError.DECODE_FAILED };
}
if (response?.isSuccess) { if (response?.isSuccess) {
recipients.some((recipient) => recipient.equals(peer.id)) || recipients.some((recipient) => recipient.equals(peer.id)) ||
recipients.push(peer.id); recipients.push(peer.id);
} else { } else {
log("Remote peer fault: No response in PushRPC"); log("Remote peer fault: No response in PushRPC");
error = SendError.REMOTE_PEER_FAULT; return { recipients, error: SendError.REMOTE_PEER_FAULT };
}
} catch (err) {
log("Failed to decode push reply", err);
error = SendError.DECODE_FAILED;
}
} catch (err) {
log("Failed to send waku light push request", err);
error = SendError.GENERIC_FAIL;
} }
return { recipients, error }; return { recipients };
}); });
const results = await Promise.allSettled(promises); const results = await Promise.allSettled(promises);