From fee89af9702ec9bd35f4d072dbd908680aa3cafc Mon Sep 17 00:00:00 2001 From: adrianEffe Date: Wed, 3 Jul 2024 13:42:01 +0200 Subject: [PATCH] propagate errors, remove unwraps --- src/application.rs | 9 ++++++--- src/main.rs | 6 ++++-- tests/integration_test.rs | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/application.rs b/src/application.rs index 63d5365..bcf4492 100644 --- a/src/application.rs +++ b/src/application.rs @@ -12,6 +12,7 @@ use crate::{ }, helper::config::Config, }; +use anyhow::Result; use axum::{ middleware, routing::{get, post}, @@ -26,8 +27,8 @@ pub struct AppState { pub auth_service: AS, } -pub async fn run(listener: TcpListener, config: Config) { - let postgres = PostgresDB::new(&config.database_url).await.unwrap(); //TODO: handle unwrap +pub async fn run(listener: TcpListener, config: Config) -> Result<()> { + let postgres = PostgresDB::new(&config.database_url).await?; let redis = RedisCache::new(&config.redis_url); let service = Service { @@ -41,7 +42,9 @@ pub async fn run(listener: TcpListener, config: Config) { }); let app = app(app_state); - axum::serve(listener, app).await.unwrap(); //TODO: handle unwrap + axum::serve(listener, app).await?; + + Ok(()) } fn app(app_state: Arc>) -> Router { diff --git a/src/main.rs b/src/main.rs index d15eb8f..9125a80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ +use anyhow::Result; use authentication_service::{application::run, helper::config::Config}; use dotenv::dotenv; use tokio::net::TcpListener; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] -async fn main() { +async fn main() -> Result<()> { tracing_subscriber::registry() .with(tracing_subscriber::fmt::layer()) .init(); @@ -14,5 +15,6 @@ async fn main() { let config = Config::init(); let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); - run(listener, config).await; + run(listener, config).await?; + Ok(()) } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 36927f0..a7ea93e 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -307,7 +307,7 @@ async fn spawn_server() -> SocketAddr { let config = Config::init(); tokio::spawn(async move { - run(listener, config).await; + run(listener, config).await.expect("Failed to run app"); }); address