diff --git a/src/tags.rs b/src/tags.rs index f817864..234b1cc 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -11,7 +11,7 @@ use rusqlite::{ use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{ISE_MSG, database}; +use crate::database::{self, DatabaseError}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Tag { @@ -61,25 +61,21 @@ pub enum TagError { #[error("No tag found with name {0}")] NoTagWithName(TagName), #[error("Database error: {0}")] - DatabaseError(String), + DatabaseError(#[from] DatabaseError), } impl From for TagError { fn from(error: rusqlite::Error) -> Self { - TagError::DatabaseError(error.to_string()) + TagError::DatabaseError(DatabaseError::from(error)) } } impl IntoResponse for TagError { fn into_response(self) -> Response { match self { - Self::DatabaseError(e) => { - log::error!("[ERROR] Database error occured: {e}"); - (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) - } - Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::NoTagWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::NoTagWithName(_) => (StatusCode::BAD_REQUEST, self.to_string()), + Self::DatabaseError(e) => e.into_response(), + Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), + Self::NoTagWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), + Self::NoTagWithName(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), } - .into_response() } } diff --git a/src/users/auth/implementation.rs b/src/users/auth/implementation.rs index f61d207..eeb0dbf 100644 --- a/src/users/auth/implementation.rs +++ b/src/users/auth/implementation.rs @@ -11,7 +11,8 @@ use rusqlite::OptionalExtension; use uuid::Uuid; use crate::{ - ISE_MSG, database, + ISE_MSG, + database::{self, DatabaseError}, users::{ User, auth::{ @@ -44,17 +45,19 @@ impl IntoResponse for AuthError { Self::InvalidFormat => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::InvalidBase64(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::InvalidUtf8(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), - Self::DatabaseError(e) => { - log::error!("[ERROR] Database error occured: {e}"); - (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response() - } + Self::DatabaseError(e) => e.into_response(), Self::PassHashError(e) => { - log::error!("[ERROR] A passwordhash error occured: {e}"); + log::error!("[PASSHASH] A passwordhash error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response() } } } } +impl From for AuthError { + fn from(value: rusqlite::Error) -> Self { + AuthError::DatabaseError(DatabaseError::from(value)) + } +} impl UserAuthRequired for Option { fn required(self) -> Result { diff --git a/src/users/auth/mod.rs b/src/users/auth/mod.rs index 309faee..c6432d4 100644 --- a/src/users/auth/mod.rs +++ b/src/users/auth/mod.rs @@ -4,9 +4,12 @@ use argon2::{Argon2, PasswordHasher, password_hash::SaltString}; use axum::http::HeaderMap; use rand08::{RngCore, rngs::OsRng}; -use crate::users::{ - User, UserError, - sessions::{Session, SessionError}, +use crate::{ + database::DatabaseError, + users::{ + User, UserError, + sessions::{Session, SessionError}, + }, }; pub mod implementation; @@ -64,7 +67,7 @@ pub enum AuthError { #[error("Invalid UTF-8 in credentials")] InvalidUtf8(#[from] std::string::FromUtf8Error), #[error("Database error: {0}")] - DatabaseError(#[from] rusqlite::Error), + DatabaseError(#[from] DatabaseError), #[error("Argon2 passhash error: {0}")] PassHashError(argon2::password_hash::Error), } diff --git a/src/users/mod.rs b/src/users/mod.rs index 31e8322..b13cfed 100644 --- a/src/users/mod.rs +++ b/src/users/mod.rs @@ -8,7 +8,7 @@ use uuid::Uuid; use crate::{ ISE_MSG, - database::{self}, + database::{self, DatabaseError}, users::{ auth::UserPasswordHashing, handle::{UserHandle, UserHandleError}, @@ -37,8 +37,8 @@ pub enum UserError { NoUserWithHandle(UserHandle), #[error("A user with handle {0} already exists")] HandleAlreadyExists(UserHandle), - #[error("Database error: {0}")] - DatabaseError(String), + #[error("{0}")] + DatabaseError(#[from] DatabaseError), #[error("Argon2 passhash error: {0}")] PassHashError(argon2::password_hash::Error), } @@ -192,7 +192,7 @@ impl User { impl From for UserError { fn from(error: rusqlite::Error) -> Self { - UserError::DatabaseError(error.to_string()) + UserError::DatabaseError(DatabaseError::from(error)) } } impl From for UserError { @@ -203,19 +203,19 @@ impl From for UserError { impl IntoResponse for UserError { fn into_response(self) -> Response { match self { - Self::DatabaseError(e) => { - log::error!("[ERROR] Database error occured: {e}"); - (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) - } + Self::DatabaseError(e) => e.into_response(), Self::PassHashError(e) => { - log::error!("[ERROR] A passwordhash error occured: {e}"); - (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) + log::error!("[PASSHASH] A passwordhash error occured: {e}"); + (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response() + } + Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), + Self::NoUserWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), + Self::NoUserWithHandle(_) => { + (StatusCode::BAD_REQUEST, self.to_string()).into_response() + } + Self::HandleAlreadyExists(_) => { + (StatusCode::CONFLICT, self.to_string()).into_response() } - Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::NoUserWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::NoUserWithHandle(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::HandleAlreadyExists(_) => (StatusCode::CONFLICT, self.to_string()), } - .into_response() } } diff --git a/src/users/sessions.rs b/src/users/sessions.rs index 0144f1e..39c05c0 100644 --- a/src/users/sessions.rs +++ b/src/users/sessions.rs @@ -9,7 +9,7 @@ use sha2::{Digest, Sha256}; use uuid::Uuid; use crate::{ - ISE_MSG, database, + database::{self, DatabaseError}, users::{User, auth}, }; @@ -34,10 +34,10 @@ pub enum SessionStatus { }, } -#[derive(Debug, thiserror::Error, Serialize)] +#[derive(Debug, thiserror::Error)] pub enum SessionError { #[error("Database error: {0}")] - DatabaseError(String), + DatabaseError(#[from] DatabaseError), #[error("No session found with id: {0}")] NoSessionWithId(Uuid), #[error("No session found with token: {0}")] @@ -45,20 +45,18 @@ pub enum SessionError { } impl From for SessionError { fn from(error: rusqlite::Error) -> Self { - SessionError::DatabaseError(error.to_string()) + SessionError::DatabaseError(DatabaseError::from(error)) } } impl IntoResponse for SessionError { fn into_response(self) -> Response { match self { - Self::DatabaseError(e) => { - log::error!("[ERROR] Database error occured: {e}"); - (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) + Self::DatabaseError(e) => e.into_response(), + Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), + Self::NoSessionWithToken(_) => { + (StatusCode::BAD_REQUEST, self.to_string()).into_response() } - Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()), - Self::NoSessionWithToken(_) => (StatusCode::BAD_REQUEST, self.to_string()), } - .into_response() } }