From 3cf886538e71bac989dd8acb85b506b2dab2e9c8 Mon Sep 17 00:00:00 2001 From: Ignacio Duart Date: Sun, 9 Jun 2024 09:09:47 +0200 Subject: [PATCH] Change some errors to anyhow --- crates/core/src/bin/freenet.rs | 18 ++++++++++-------- crates/core/src/config.rs | 11 ++++++----- crates/core/src/node.rs | 3 +-- crates/core/src/server.rs | 6 ++---- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/core/src/bin/freenet.rs b/crates/core/src/bin/freenet.rs index fe3af155a..c6e7ffabf 100644 --- a/crates/core/src/bin/freenet.rs +++ b/crates/core/src/bin/freenet.rs @@ -6,31 +6,33 @@ use freenet::{ }; use std::{net::SocketAddr, sync::Arc}; -type DynError = Box; - -async fn run(config: Config) -> Result<(), DynError> { +async fn run(config: Config) -> Result<(), anyhow::Error> { match config.mode { OperationMode::Local => run_local(config).await, OperationMode::Network => run_network(config).await, } } -async fn run_local(config: Config) -> Result<(), DynError> { +async fn run_local(config: Config) -> Result<(), anyhow::Error> { tracing::info!("Starting freenet node in local mode"); let port = config.ws_api.port; let ip = config.ws_api.address; - let executor = Executor::from_config(Arc::new(config), None).await?; + let executor = Executor::from_config(Arc::new(config), None) + .await + .map_err(anyhow::Error::msg)?; let socket: SocketAddr = (ip, port).into(); - run_local_node(executor, socket).await + run_local_node(executor, socket) + .await + .map_err(anyhow::Error::msg) } -async fn run_network(config: Config) -> Result<(), DynError> { +async fn run_network(config: Config) -> Result<(), anyhow::Error> { tracing::info!("Starting freenet node in network mode"); run_network_node(config).await } -fn main() -> Result<(), DynError> { +fn main() -> Result<(), anyhow::Error> { freenet::config::set_logger(None); let config = ConfigArgs::parse().build()?; let rt = tokio::runtime::Builder::new_multi_thread() diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index f023c4bb7..0bb7e7bd4 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -102,11 +102,11 @@ impl ConfigArgs { } let filename = e.file_name().to_string_lossy().into_owned(); let ext = filename.rsplit('.').next().map(|s| s.to_owned()); - tracing::info!("Found configuration file: {filename}, {ext:?}"); if let Some(ext) = ext { if filename.starts_with("config") { match ext.as_str() { "toml" => { + tracing::info!("Found configuration file: {filename}.{ext}"); return Some((filename, ext)); } "json" => { @@ -202,12 +202,13 @@ impl ConfigArgs { if self.config_paths.data_dir.is_none() { self.config_paths.data_dir = Some(data); } - Self::read_config(&config)? + Self::read_config(&config)?.map(|cfg| { + tracing::info!("Found configuration file in default directory"); + cfg + }) }; - if cfg.is_some() { - tracing::debug!("Found configuration file in default directory"); - } + if cfg.is_some() {} let should_persist = cfg.is_none(); diff --git a/crates/core/src/node.rs b/crates/core/src/node.rs index c1fe9c3ee..a007dcf96 100644 --- a/crates/core/src/node.rs +++ b/crates/core/src/node.rs @@ -44,7 +44,6 @@ use crate::{ ring::{Location, PeerKeyLocation}, router::{RouteEvent, RouteOutcome}, tracing::{EventRegister, NetEventLog, NetEventRegister}, - DynError, }; use crate::{ config::Config, @@ -66,7 +65,7 @@ pub(crate) mod testing_impl; pub struct Node(NodeP2P); impl Node { - pub async fn run(self) -> Result<(), DynError> { + pub async fn run(self) -> Result<(), anyhow::Error> { self.0.run_node().await?; Ok(()) } diff --git a/crates/core/src/server.rs b/crates/core/src/server.rs index 3aba69e16..ec46bc938 100644 --- a/crates/core/src/server.rs +++ b/crates/core/src/server.rs @@ -182,13 +182,11 @@ pub mod local_node { pub mod network_node { use tower_http::trace::TraceLayer; - use crate::{ - client_events::websocket::WebSocketProxy, config::Config, dev_tool::NodeConfig, DynError, - }; + use crate::{client_events::websocket::WebSocketProxy, config::Config, dev_tool::NodeConfig}; use super::{http_gateway::HttpGateway, serve}; - pub async fn run_network_node(config: Config) -> Result<(), DynError> { + pub async fn run_network_node(config: Config) -> Result<(), anyhow::Error> { let ws_socket = (config.ws_api.address, config.ws_api.port).into(); let (gw, gw_router) = HttpGateway::as_router(&ws_socket); let (ws_proxy, ws_router) = WebSocketProxy::as_router(gw_router);