From e9a40c7365c51b8b82334cc26ff6096c3b050ae9 Mon Sep 17 00:00:00 2001 From: necipsagiro Date: Fri, 1 Mar 2024 15:07:23 +0300 Subject: [PATCH] Fix error handling --- src-tauri/src/main.rs | 5 ++- src/node-page.js | 99 ++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 08be441..8259d85 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -938,16 +938,17 @@ fn edit_validator( --security-contact={contact} \ --identity={keybase_id} \ --commission-rate={com_rate} \ - --details={details}'", + --details={details} 2>&1'", password = my_boxed_session.walletpassword, operation = match exception.as_str() { - "babylon" => "checkpointing", + "babylon" => "staking", _ => "staking", }, )) .map_err(|e| e.to_string())?; channel.read_to_string(&mut s).map_err(|e| e.to_string())?; channel.close().map_err(|e| e.to_string())?; + println!("{}", s); Ok(s) } diff --git a/src/node-page.js b/src/node-page.js index 98aa6e7..0370cd3 100644 --- a/src/node-page.js +++ b/src/node-page.js @@ -94,6 +94,15 @@ tevent.listen("check_logs", (event) => { } }); +function getJsonFromText(text) { + const startIndex = text.indexOf('{'); + const endIndex = text.lastIndexOf('}') + 1; + + const json = text.substring(startIndex, endIndex); + + return JSON.parse(json); +}; + const loadNodePage = async (start) => { const eachSidebarTag = document.querySelectorAll(".each-sidebar-tag"); eachSidebarTag[0].setAttribute("class", "each-sidebar-tag sidebar-active-tag"); @@ -501,26 +510,33 @@ const addValidatorSetup = () => { details: document.querySelectorAll(".each-input-field")[8].value, exception: exception }).then(async (res) => { - console.log(res); - res = res.slice(res.indexOf('{')); - res = JSON.parse(res); - if (res.raw_log.length == 2) { - createMessage("Tx Hash", res.txhash); - await tauri.invoke("show_wallets", { exception: exception }).then(async (list) => { - list = JSON.parse(list); - currentIp.validator_addr = list.filter((item) => item.name == document.querySelectorAll(".each-input-field")[1].value)[0].address; - }).catch((err) => { - console.log(err); - showErrorMessage(err); - }); - await tauri.invoke("set_main_wallet", { walletname: document.querySelectorAll(".each-input-field")[1].value, address: currentIp.validator_addr, exception: exception }).catch((err) => { - console.log(err); - showErrorMessage(err); - }); - localStorage.setItem("ipaddresses", JSON.stringify(ipAddresses)); - await updateSidebar(); - } else { - showErrorMessage(res.raw_log); + if (res.includes('validator already exist')) + showErrorMessage("Validator already exists"); + else if (res.includes('insufficient funds')) + showErrorMessage("Insufficient funds"); + else if (res.includes('not found: key not found')) + showErrorMessage("Please send tokens to the wallet first"); + else { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { + createMessage("Tx Hash", res.txhash); + await tauri.invoke("show_wallets", { exception: exception }).then(async (list) => { + list = JSON.parse(list); + currentIp.validator_addr = list.filter((item) => item.name == document.querySelectorAll(".each-input-field")[1].value)[0].address; + }).catch((err) => { + console.log(err); + showErrorMessage(err); + }); + await tauri.invoke("set_main_wallet", { walletname: document.querySelectorAll(".each-input-field")[1].value, address: currentIp.validator_addr, exception: exception }).catch((err) => { + console.log(err); + showErrorMessage(err); + }); + localStorage.setItem("ipaddresses", JSON.stringify(ipAddresses)); + await updateSidebar(); + } else { + showErrorMessage(res.raw_log); + }; }; }).catch(async (err) => { await handleTimeOut(err); showErrorMessage(err); }); hideLoadingAnimation(); @@ -537,17 +553,16 @@ const editValidatorSetup = () => { hideErrorMessage(); await tauri.invoke("edit_validator", { walletName: document.querySelectorAll(".each-input-field")[0].value, - website: document.querySelectorAll(".each-input-field")[1].value, - comRate: document.querySelectorAll(".each-input-field")[2].value, - contact: document.querySelectorAll(".each-input-field")[3].value, - keybaseId: document.querySelectorAll(".each-input-field")[4].value, + comRate: document.querySelectorAll(".each-input-field")[1].value, + website: document.querySelectorAll(".each-input-field")[2].value, + keybaseId: document.querySelectorAll(".each-input-field")[3].value, + contact: document.querySelectorAll(".each-input-field")[4].value, details: document.querySelectorAll(".each-input-field")[5].value, exception: exception }).then((res) => { - res = res.slice(res.indexOf('{')); console.log(res); - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -569,8 +584,9 @@ const withdrawRewardsSetup = () => { fees: document.querySelectorAll(".each-input-field")[1].value, exception: exception }).then((res) => { - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -592,9 +608,9 @@ const delegateSetup = (valoper) => { amount: document.querySelectorAll(".each-input-field")[3].value, exception: exception }).then((res) => { + res = getJsonFromText(res); console.log(res); - res = JSON.parse(res); - if (res.raw_log.length == 2) { + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -616,8 +632,9 @@ const redelegateSetup = () => { amount: document.querySelectorAll(".each-input-field")[4].value, exception: exception }).then((res) => { - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -637,8 +654,9 @@ const voteSetup = () => { selectedOption: document.querySelector(".each-input-radio-option:checked").nextElementSibling.textContent.toLowerCase(), exception: exception }).then((res) => { - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -657,8 +675,9 @@ const unjailSetup = () => { fees: document.querySelectorAll(".each-input-field")[1].value, exception: exception }).then((res) => { - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -679,8 +698,9 @@ const sendTokenSetup = () => { fees: document.querySelectorAll(".each-input-field")[3].value, exception: exception }).then((res) => { - res = JSON.parse(res); - if (res.raw_log.length == 2) { + res = getJsonFromText(res); + console.log(res); + if (res.raw_log.length == 2 || res.raw_log.length == 0) { createMessage("Tx Hash", res.txhash); } else { showErrorMessage(res.raw_log); @@ -695,6 +715,7 @@ const createBLSKeySetup = () => { showLoadingAnimation(); hideErrorMessage(); await tauri.invoke("create_bls_key", { walletName: document.querySelectorAll(".each-input-field")[0].value }).then((res) => { + res = getJsonFromText(res); console.log(res); if (res.includes("Error")) { showErrorMessage("Wrong wallet name!");