From e4fda1a806785c4d2b44731364b91b680443c76b Mon Sep 17 00:00:00 2001 From: danda Date: Wed, 22 May 2024 18:34:47 -0700 Subject: [PATCH] refactor: add anyhow, remove unwrap/expect converts unwrap and expect calls in main.rs into anyhow .with_context() calls to provide better error messages --- Cargo.lock | 5 +++-- Cargo.toml | 3 ++- src/main.rs | 48 +++++++++++++++++++++++++++++------------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd856c3..ea5517f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,9 +150,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.82" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "arbitrary" @@ -1819,6 +1819,7 @@ dependencies = [ name = "neptune-explorer" version = "0.1.0" dependencies = [ + "anyhow", "axum 0.7.5", "boilerplate", "clap", diff --git a/Cargo.toml b/Cargo.toml index abb68cf..f00952d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,13 +21,14 @@ tarpc = { version = "^0.34", features = [ ] } clap = "4.5.4" thiserror = "1.0.59" -#boilerplate = { version = "1.0.0", features = ["axum"] } boilerplate = { version = "1.0.0" } html-escaper = "0.2.0" tower-http = { version = "0.5.2", features = ["fs"] } readonly = "0.2.12" url = "2.5.0" +# only should be used inside main.rs, for the binary. +anyhow = "1.0.86" [patch.crates-io] # 694f27daf78aade0ed0dc07e3babaab036cd5572 is tip of branch: master as of 2024-04-30 diff --git a/src/main.rs b/src/main.rs index 6e49700..4e1ad53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use axum::routing::get; use axum::routing::Router; use clap::Parser; @@ -17,19 +18,24 @@ use std::net::Ipv4Addr; use std::net::SocketAddr; use std::sync::Arc; use tarpc::client; -use tarpc::client::RpcError; use tarpc::context; use tarpc::tokio_serde::formats::Json as RpcJson; use tower_http::services::ServeDir; #[tokio::main] -async fn main() -> Result<(), RpcError> { - let rpc_client = rpc_client().await; - let network = rpc_client.network(context::current()).await?; +async fn main() -> Result<(), anyhow::Error> { + let rpc_client = rpc_client() + .await + .with_context(|| "Failed to create RPC client")?; + let network = rpc_client + .network(context::current()) + .await + .with_context(|| "Failed calling neptune-core api: network")?; let genesis_digest = rpc_client .block_digest(context::current(), BlockSelector::Genesis) - .await? - .expect("Genesis block should be found"); + .await + .with_context(|| "Failed calling neptune-core api: block_digest")? + .with_context(|| "neptune-core failed to provide a genesis block")?; let shared_state = Arc::new(AppState::from(( network, @@ -63,23 +69,27 @@ async fn main() -> Result<(), RpcError> { // add state .with_state(shared_state); - println!("Running on http://localhost:3000"); - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); - axum::serve(listener, app).await.unwrap(); + let port = 3000; + let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}")) + .await + .with_context(|| format!("Failed to bind to port {port}"))?; + + println!("Running on http://localhost:{port}"); + + axum::serve(listener, app) + .await + .with_context(|| "Axum server encountered an error")?; Ok(()) } -async fn rpc_client() -> RPCClient { +async fn rpc_client() -> Result { // Create connection to neptune-core RPC server let args: Config = Config::parse(); let server_socket = SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::LOCALHOST), args.port); - let transport = tarpc::serde_transport::tcp::connect(server_socket, RpcJson::default).await; - let transport = match transport { - Ok(transp) => transp, - Err(err) => { - eprintln!("{err}"); - panic!("Connection to neptune-core failed. Is a node running?"); - } - }; - RPCClient::new(client::Config::default(), transport).spawn() + let transport = tarpc::serde_transport::tcp::connect(server_socket, RpcJson::default) + .await + .with_context(|| { + format!("Failed to connect to neptune-core rpc service at {server_socket}") + })?; + Ok(RPCClient::new(client::Config::default(), transport).spawn()) }