From 0131136e498f79d4ed281a37651b949e3e6ddbf4 Mon Sep 17 00:00:00 2001 From: Kaung Myat Thu <2962000kaungmyatthu@gmail.com> Date: Tue, 16 Jul 2024 17:55:06 +0700 Subject: [PATCH] fix: ts side of error callback now know how to handle err fix: error array index skipping --- shardus_net/src/lib.rs | 6 ++++-- src/index.ts | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/shardus_net/src/lib.rs b/shardus_net/src/lib.rs index 23d8b09..11b7b8d 100644 --- a/shardus_net/src/lib.rs +++ b/shardus_net/src/lib.rs @@ -340,13 +340,15 @@ pub fn multi_send_with_header(mut cx: FunctionContext) -> JsResult nodejs_thread_channel.send(move |mut cx| { let cx = &mut cx; + let stats = this.to_inner(cx).get::>, _, _>(cx, "_stats")?; let js_arr = cx.empty_array(); + let mut error_count = 0; for i in 0..results.len() { - let stats = this.to_inner(cx).get::>, _, _>(cx, "_stats")?; (**stats).borrow_mut().decrement_outstanding_sends(); if let Err(err) = &results[i] { let err = cx.string(format!("{:?}", err)); - js_arr.set(cx, i as u32, err)?; + js_arr.set(cx, error_count, err)?; + error_count += 1; } } diff --git a/src/index.ts b/src/index.ts index 87ff4a4..e29d018 100644 --- a/src/index.ts +++ b/src/index.ts @@ -148,11 +148,22 @@ export const Sn = (opts: SnOpts) => { : null /* prettier-ignore */ if(logFlags.net_verbose) logMessageInfo(augData, stringifiedData) - const sendCallback = (error) => { + const multiSendCallback = (error: string[]) => { + if (error.length == address.length) { + throw new Error(`_sendAug: request_id: ${augData.UUID} error sending from rust failure lib-net: ${error.join(', ')}`) + } + if (error.length > 0) { + return resolve({ success: false, error: error.join(', ') }) + } + return resolve({ success: true }) + } + + const sendCallback = (error?: string) => { if (error) { resolve({ success: false, error }) - } else { + }else { resolve({ success: true }) + } } try { @@ -167,7 +178,7 @@ export const Sn = (opts: SnOpts) => { optionalHeader.version, stringifiedHeader, stringifiedData, - sendCallback, + multiSendCallback, callbackEnabled ) } else {