From adbcf969a3b83e5d8d176cb13ab591e6f325430f Mon Sep 17 00:00:00 2001 From: Nima Date: Wed, 3 May 2023 16:34:56 +0200 Subject: [PATCH] #52 Improve the error message of the node status command (#79) * #52 Improve the error message of the node status command in case of connection issues * Add panic handler for all the cli errors --- src/cli/mod.rs | 22 +++++++++++++++++++++- src/cli/node/status.rs | 5 ++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index efe24b8f..86b86439 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -19,7 +19,6 @@ use { bazuka::core::{Address, Decimal, GeneralAddress, MpnAddress, TokenId}, bazuka::mpn::MpnWorker, bazuka::wallet::WalletCollection, - colored::Colorize, serde::{Deserialize, Serialize}, std::net::SocketAddr, std::path::{Path, PathBuf}, @@ -27,6 +26,12 @@ use { tokio::try_join, }; +use { + colored::Colorize, + std::io::{self, Write}, + std::panic, +}; + pub mod chain; pub mod init; pub mod wallet; @@ -380,6 +385,21 @@ pub async fn initialize_cli() { let wallet_path = home::home_dir().unwrap().join(Path::new(".bazuka-wallet")); let wallet = WalletCollection::open(wallet_path.clone()).unwrap(); + panic::set_hook(Box::new(|panic_info| { + let default_message = "Unknown panic"; + + let message = panic_info + .payload() + .downcast_ref::<&str>() + .unwrap_or(&default_message); + + let stderr = io::stderr(); + let mut stderr_handle = stderr.lock(); + write!(stderr_handle, "{} ", "Error:".bold().red()).unwrap(); + stderr_handle.write_all(message.as_bytes()).unwrap(); + stderr_handle.write_all(b"\n").unwrap(); + })); + match opts { CliOptions::Chain(chain_opts) => match chain_opts { ChainCliOptions::Rollback {} => { diff --git a/src/cli/node/status.rs b/src/cli/node/status.rs index 0806a59b..8d09bc85 100644 --- a/src/cli/node/status.rs +++ b/src/cli/node/status.rs @@ -18,7 +18,10 @@ pub async fn status(conf: BazukaConfig, mut wallet: WalletCollection) { ); try_join!( async move { - println!("{:#?}", client.stats().await?); + match client.stats().await.ok() { + Some(resp) => println!("{:#?}", resp), + None => panic!("Could not receive stats from the node. Make sure the node is started and running.") + } Ok::<(), NodeError>(()) }, req_loop