From 6b8cccede013e3f1cd8d2bde7508e922cca44c5e Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Mon, 10 May 2021 09:34:38 +0200 Subject: [PATCH] Introduce custom errors --- src/domain/error.rs | 11 +++++++++++ src/domain/handler.rs | 8 ++++---- src/domain/mod.rs | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/domain/error.rs diff --git a/src/domain/error.rs b/src/domain/error.rs new file mode 100644 index 0000000..8a8c353 --- /dev/null +++ b/src/domain/error.rs @@ -0,0 +1,11 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Authentication error for `{0}`")] + AuthenticationError(String), + #[error("Database error")] + DatabaseError(#[from] sqlx::Error), +} + +pub type Result = std::result::Result; diff --git a/src/domain/handler.rs b/src/domain/handler.rs index 110c79c..1af5ff0 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -1,7 +1,6 @@ use super::sql_tables::*; -use crate::domain::sql_tables::Pool; +use crate::domain::{error::*, sql_tables::Pool}; use crate::infra::configuration::Configuration; -use anyhow::{bail, Result}; use async_trait::async_trait; use futures_util::StreamExt; use futures_util::TryStreamExt; @@ -61,7 +60,8 @@ impl BackendHandler for SqlBackendHandler { if request.password == self.config.ldap_user_pass { return Ok(()); } else { - bail!(r#"Authentication error for "{}""#, request.name) + debug!(r#"Invalid password for LDAP bind user"#); + return Err(Error::AuthenticationError(request.name)); } } let query = Query::select() @@ -78,7 +78,7 @@ impl BackendHandler for SqlBackendHandler { } else { debug!(r#"No user found for "{}""#, request.name); } - bail!(r#"Authentication error for "{}""#, request.name) + Err(Error::AuthenticationError(request.name)) } async fn list_users(&self, request: ListUsersRequest) -> Result> { diff --git a/src/domain/mod.rs b/src/domain/mod.rs index d3f7dbb..00320b6 100644 --- a/src/domain/mod.rs +++ b/src/domain/mod.rs @@ -1,2 +1,3 @@ +pub mod error; pub mod handler; pub mod sql_tables;