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) => {
let error: SendError | undefined;
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 };
return { recipients, error: SendError.REMOTE_PEER_FAULT };
}
let res: Uint8ArrayList[] | undefined;
try {
const res = await pipe(
res = await pipe(
[query.encode()],
lp.encode,
stream,
lp.decode,
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();
res.forEach((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) {
recipients.some((recipient) => recipient.equals(peer.id)) ||
recipients.push(peer.id);
} else {
log("Remote peer fault: No response in PushRPC");
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: SendError.REMOTE_PEER_FAULT };
}
return { recipients, error };
return { recipients };
});
const results = await Promise.allSettled(promises);