some_auth/
error.rs

1use std::fmt;
2
3use bcrypt::BcryptError;
4
5/// Errors connected to auth mechanisms
6#[derive(Debug)]
7pub enum AuthError {
8    /// Auth problem connected with some internal error
9    Internal(String),
10    /// Validation error in auth' domain
11    ValidationError(String),
12    /// Provided username is unavailable
13    UsernameUnavailable,
14    /// Invalid credentials. 
15    /// This module returns such error in most cases when there are some problems with user validation 
16    /// to provide less information for potential intruder
17    InvalidCredentials,
18    /// Error connected with `UserRepository`
19    AuthRepositoryError(String),
20    /// User not found. Inner string represents info about user
21    UserNotFound(String),
22    /// Current operation is invalid
23    InvalidOperation(String),
24    /// User is unathorized
25    Unathorized,
26    /// Role with provided name is already exists
27    RoleAlreadyExists,
28    /// Role not found. Inner string represents info about role
29    RoleNotFound(String)
30}
31
32impl fmt::Display for AuthError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            AuthError::Internal(err) => write!(f, "Auth internal error: {err}"),
36            AuthError::ValidationError(err) => write!(f, "Auth validation error: {err}"),
37            AuthError::UsernameUnavailable => write!(f, "Provided username is unavailable"),
38            AuthError::InvalidCredentials => write!(f, "Invalid credentials"),
39            AuthError::AuthRepositoryError(err) => write!(f, "Auth repository error: {err}"),
40            AuthError::UserNotFound(username_or_id) => write!(f, "Couldn't find user {username_or_id}"),
41            AuthError::InvalidOperation(message) => write!(f, "Invalid auth operation: {message}"),
42            AuthError::Unathorized => write!(f, "Unathorized"),
43            AuthError::RoleAlreadyExists => write!(f, "Role already exists"),
44            AuthError::RoleNotFound(username_or_id) => write!(f, "Couldn't find role {username_or_id}"),
45        }
46    }
47}
48
49impl From<BcryptError> for AuthError {
50    fn from(error: BcryptError) -> Self {
51        AuthError::Internal(error.to_string())
52    }
53}