Skip to content

Commit

Permalink
#52 Improve the error message of the node status command (#79)
Browse files Browse the repository at this point in the history
* #52 Improve the error message of the node status command in case of connection issues

* Add panic handler for all the cli errors
  • Loading branch information
n1rna committed May 3, 2023
1 parent 85a50be commit adbcf96
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ 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},
structopt::StructOpt,
tokio::try_join,
};

use {
colored::Colorize,
std::io::{self, Write},
std::panic,
};

pub mod chain;
pub mod init;
pub mod wallet;
Expand Down Expand Up @@ -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 {} => {
Expand Down
5 changes: 4 additions & 1 deletion src/cli/node/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit adbcf96

Please sign in to comment.