From befbd37b6ff24033299fcb9a8a69fcb66dbbf03e Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 17 Aug 2023 18:09:32 +0530 Subject: [PATCH] add try catch --- package-lock.json | 2 +- packages/core/src/lib/light_push/index.ts | 33 ++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index c22c27b078..5ef1ed9841 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35752,7 +35752,7 @@ "@waku/core": "0.0.22", "@waku/dns-discovery": "0.0.16", "@waku/interfaces": "0.0.17", - "@waku/peer-exchange": "*", + "@waku/peer-exchange": "^0.0.15", "@waku/relay": "0.0.5", "@waku/utils": "0.0.10", "cspell": "^7.0.0", diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index e3c6d2da5c..e5ba7b4f04 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -47,26 +47,35 @@ class LightPush extends BaseProtocol implements ILightPush { const { pubSubTopic = DefaultPubSubTopic } = this.options; const recipients: PeerId[] = []; + let query: PushRpc; + + try { + if (!isSizeValid(message.payload)) { + log("Failed to send waku light push: message is bigger than 1MB"); + return { + recipients, + errors: [SendError.SIZE_TOO_BIG] + }; + } - if (!isSizeValid(message.payload)) { - log("Failed to send waku light push: message is bigger than 1MB"); - return { - recipients, - errors: [SendError.SIZE_TOO_BIG] - }; - } + const protoMessage = await encoder.toProtoObj(message); + if (!protoMessage) { + log("Failed to encode to protoMessage, aborting push"); + return { + recipients, + errors: [SendError.ENCODE_FAILED] + }; + } - const protoMessage = await encoder.toProtoObj(message); - if (!protoMessage) { - log("Failed to encode to protoMessage, aborting push"); + query = PushRpc.createRequest(protoMessage, pubSubTopic); + } catch (error) { + log("Failed to encode to protoMessage", error); return { recipients, errors: [SendError.ENCODE_FAILED] }; } - const query = PushRpc.createRequest(protoMessage, pubSubTopic); - const peers = await this.getPeers(3, true, opts?.peerId && [opts?.peerId]); const promises = peers.map(async (peer) => {