From a31b6e472e0087f017a938bb6858d53f2a37f1de Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 20 Sep 2023 16:02:52 +1000 Subject: [PATCH] refactor: Remove nest of try/catch in favor of sequential try/catch --- packages/core/src/lib/light_push/index.ts | 52 +++++++++++------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index 4ecdea0be4..de78c3b144 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -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 { - const bytes = new Uint8ArrayList(); - res.forEach((chunk) => { - bytes.append(chunk); - }); - - const response = PushRpc.decode(bytes).response; - - 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.GENERIC_FAIL }; } - return { recipients, error }; + const bytes = new Uint8ArrayList(); + res.forEach((chunk) => { + bytes.append(chunk); + }); + + 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"); + return { recipients, error: SendError.REMOTE_PEER_FAULT }; + } + + return { recipients }; }); const results = await Promise.allSettled(promises);