refactor: add anyhow, remove unwrap/expect

converts unwrap and expect calls in main.rs into anyhow .with_context()
calls to provide better error messages
This commit is contained in:
danda 2024-05-22 18:34:47 -07:00
parent 7dfe9366be
commit e4fda1a806
3 changed files with 34 additions and 22 deletions

5
Cargo.lock generated
View File

@ -150,9 +150,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.82" version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]] [[package]]
name = "arbitrary" name = "arbitrary"
@ -1819,6 +1819,7 @@ dependencies = [
name = "neptune-explorer" name = "neptune-explorer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"axum 0.7.5", "axum 0.7.5",
"boilerplate", "boilerplate",
"clap", "clap",

View File

@ -21,13 +21,14 @@ tarpc = { version = "^0.34", features = [
] } ] }
clap = "4.5.4" clap = "4.5.4"
thiserror = "1.0.59" thiserror = "1.0.59"
#boilerplate = { version = "1.0.0", features = ["axum"] }
boilerplate = { version = "1.0.0" } boilerplate = { version = "1.0.0" }
html-escaper = "0.2.0" html-escaper = "0.2.0"
tower-http = { version = "0.5.2", features = ["fs"] } tower-http = { version = "0.5.2", features = ["fs"] }
readonly = "0.2.12" readonly = "0.2.12"
url = "2.5.0" url = "2.5.0"
# only should be used inside main.rs, for the binary.
anyhow = "1.0.86"
[patch.crates-io] [patch.crates-io]
# 694f27daf78aade0ed0dc07e3babaab036cd5572 is tip of branch: master as of 2024-04-30 # 694f27daf78aade0ed0dc07e3babaab036cd5572 is tip of branch: master as of 2024-04-30

View File

@ -1,3 +1,4 @@
use anyhow::Context;
use axum::routing::get; use axum::routing::get;
use axum::routing::Router; use axum::routing::Router;
use clap::Parser; use clap::Parser;
@ -17,19 +18,24 @@ use std::net::Ipv4Addr;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use tarpc::client; use tarpc::client;
use tarpc::client::RpcError;
use tarpc::context; use tarpc::context;
use tarpc::tokio_serde::formats::Json as RpcJson; use tarpc::tokio_serde::formats::Json as RpcJson;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), RpcError> { async fn main() -> Result<(), anyhow::Error> {
let rpc_client = rpc_client().await; let rpc_client = rpc_client()
let network = rpc_client.network(context::current()).await?; .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 let genesis_digest = rpc_client
.block_digest(context::current(), BlockSelector::Genesis) .block_digest(context::current(), BlockSelector::Genesis)
.await? .await
.expect("Genesis block should be found"); .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(( let shared_state = Arc::new(AppState::from((
network, network,
@ -63,23 +69,27 @@ async fn main() -> Result<(), RpcError> {
// add state // add state
.with_state(shared_state); .with_state(shared_state);
println!("Running on http://localhost:3000"); let port = 3000;
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}"))
axum::serve(listener, app).await.unwrap(); .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(()) Ok(())
} }
async fn rpc_client() -> RPCClient { async fn rpc_client() -> Result<RPCClient, anyhow::Error> {
// Create connection to neptune-core RPC server // Create connection to neptune-core RPC server
let args: Config = Config::parse(); let args: Config = Config::parse();
let server_socket = SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::LOCALHOST), args.port); 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 = tarpc::serde_transport::tcp::connect(server_socket, RpcJson::default)
let transport = match transport { .await
Ok(transp) => transp, .with_context(|| {
Err(err) => { format!("Failed to connect to neptune-core rpc service at {server_socket}")
eprintln!("{err}"); })?;
panic!("Connection to neptune-core failed. Is a node running?"); Ok(RPCClient::new(client::Config::default(), transport).spawn())
}
};
RPCClient::new(client::Config::default(), transport).spawn()
} }