1use std::fmt;
2
3use bcrypt::BcryptError;
4
5#[derive(Debug)]
7pub enum AuthError {
8 Internal(String),
10 ValidationError(String),
12 UsernameUnavailable,
14 InvalidCredentials,
18 AuthRepositoryError(String),
20 UserNotFound(String),
22 InvalidOperation(String),
24 Unathorized,
26 RoleAlreadyExists,
28 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}