use std::fmt;
use bcrypt::BcryptError;
#[derive(Debug)]
pub enum AuthError {
Internal(String),
ValidationError(String),
UsernameUnavailable,
InvalidCredentials,
AuthRepositoryError(String),
UserNotFound(String),
InvalidOperation(String),
Unathorized
}
impl fmt::Display for AuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AuthError::Internal(err) => write!(f, "Auth internal error: {err}"),
AuthError::ValidationError(err) => write!(f, "Auth validation error: {err}"),
AuthError::UsernameUnavailable => write!(f, "Provided username is unavailable"),
AuthError::InvalidCredentials => write!(f, "Invalid credentials"),
AuthError::AuthRepositoryError(err) => write!(f, "Auth repository error: {err}"),
AuthError::UserNotFound(username_or_id) => write!(f, "Couldn't find user {username_or_id}"),
AuthError::InvalidOperation(message) => write!(f, "Invalid auth operation: {message}"),
AuthError::Unathorized => write!(f, "Unathorized"),
}
}
}
impl From<BcryptError> for AuthError {
fn from(error: BcryptError) -> Self {
AuthError::Internal(error.to_string())
}
}