From 350fdcdf9b55ac5be1ef74a8b3673ef32f92db75 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 3 Nov 2021 16:01:55 +0900 Subject: [PATCH] server: improve error messages --- server/src/infra/ldap_server.rs | 24 +++++++++++++----------- server/src/infra/tcp_server.rs | 5 ++++- server/src/main.rs | 25 ++++++++++++++++++------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/server/src/infra/ldap_server.rs b/server/src/infra/ldap_server.rs index 1847e99..53717e6 100644 --- a/server/src/infra/ldap_server.rs +++ b/server/src/infra/ldap_server.rs @@ -8,7 +8,7 @@ use crate::{ use actix_rt::net::TcpStream; use actix_server::ServerBuilder; use actix_service::{fn_service, ServiceFactoryExt}; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result}; use futures_util::future::ok; use ldap3_server::{proto::LdapMsg, LdapCodec}; use log::*; @@ -43,9 +43,9 @@ where .context("while sending a response: {:#}")? } - if let Err(e) = resp.flush().await { - bail!("Error while flushing responses: {:?}", e); - } + resp.flush() + .await + .context("while flushing responses: {:#}")? } } Ok(true) @@ -63,8 +63,8 @@ where let ldap_base_dn = config.ldap_base_dn.clone(); let ldap_user_dn = config.ldap_user_dn.clone(); - Ok( - server_builder.bind("ldap", ("0.0.0.0", config.ldap_port), move || { + server_builder + .bind("ldap", ("0.0.0.0", config.ldap_port), move || { let backend_handler = backend_handler.clone(); let ldap_base_dn = ldap_base_dn.clone(); let ldap_user_dn = ldap_user_dn.clone(); @@ -81,7 +81,10 @@ where let mut session = LdapHandler::new(backend_handler, ldap_base_dn, ldap_user_dn); while let Some(msg) = requests.next().await { - if !handle_incoming_message(msg, &mut resp, &mut session).await? { + if !handle_incoming_message(msg, &mut resp, &mut session) + .await + .context("while handling incoming messages")? + { break; } } @@ -89,12 +92,11 @@ where Ok(stream) } }) - .map_err(|err: anyhow::Error| error!("Service Error: {:?}", err)) - // catch + .map_err(|err: anyhow::Error| error!("Service Error: {:#}", err)) .and_then(move |_| { // finally ok(()) }) - })?, - ) + }) + .with_context(|| format!("while binding to the port {}", config.ldap_port)) } diff --git a/server/src/infra/tcp_server.rs b/server/src/infra/tcp_server.rs index 68d889e..ce9ab2c 100644 --- a/server/src/infra/tcp_server.rs +++ b/server/src/infra/tcp_server.rs @@ -87,7 +87,10 @@ where Backend: TcpBackendHandler + BackendHandler + LoginHandler + OpaqueHandler + Sync + 'static, { let jwt_secret = config.jwt_secret.clone(); - let jwt_blacklist = backend_handler.get_jwt_blacklist().await?; + let jwt_blacklist = backend_handler + .get_jwt_blacklist() + .await + .context("while getting the jwt blacklist")?; server_builder .bind("http", ("0.0.0.0", config.http_port), move || { let backend_handler = backend_handler.clone(); diff --git a/server/src/main.rs b/server/src/main.rs index 9ca0b10..d45a647 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -47,27 +47,38 @@ async fn run_server(config: Configuration) -> Result<()> { let sql_pool = PoolOptions::new() .max_connections(5) .connect(&config.database_url) - .await?; - domain::sql_tables::init_table(&sql_pool).await?; + .await + .context("while connecting to the DB")?; + domain::sql_tables::init_table(&sql_pool) + .await + .context("while creating the tables")?; let backend_handler = SqlBackendHandler::new(config.clone(), sql_pool.clone()); if let Err(e) = backend_handler.get_user_details(&config.ldap_user_dn).await { warn!("Could not get admin user, trying to create it: {:#}", e); create_admin_user(&backend_handler, &config) .await - .map_err(|e| anyhow!("Error setting up admin login/account: {:#}", e))?; + .map_err(|e| anyhow!("Error setting up admin login/account: {:#}", e)) + .context("while creating the admin user")?; } let server_builder = infra::ldap_server::build_ldap_server( &config, backend_handler.clone(), actix_server::Server::build(), - )?; + ) + .context("while binding the LDAP server")?; infra::jwt_sql_tables::init_table(&sql_pool).await?; let server_builder = - infra::tcp_server::build_tcp_server(&config, backend_handler, server_builder).await?; + infra::tcp_server::build_tcp_server(&config, backend_handler, server_builder) + .await + .context("while binding the TCP server")?; // Run every hour. let scheduler = Scheduler::new("0 0 * * * * *", sql_pool); scheduler.start(); - server_builder.workers(1).run().await?; + server_builder + .workers(1) + .run() + .await + .context("while starting the server")?; Ok(()) } @@ -81,7 +92,7 @@ fn run_server_command(opts: RunOpts) -> Result<()> { debug!("Configuration: {:#?}", config); actix::run( - run_server(config).unwrap_or_else(|e| error!("Could not bring up the servers: {:?}", e)), + run_server(config).unwrap_or_else(|e| error!("Could not bring up the servers: {:#}", e)), )?; info!("End.");