wearust/
error.rs

1
2
3use thiserror::*;
4#[cfg(feature = "actix")]
5use actix_web::{HttpResponse, ResponseError, http::StatusCode};
6
7
8
9#[derive(Debug, Error)]
10pub enum AuthError {
11    #[error("Invaid Input supplied!")]
12    InvalidInput,
13    #[error("User Error!")]
14    UserError,
15    #[error("Strategy provided not found!")]
16    StrategyNotFound,
17    #[error("Wrong password, check and try again!")]
18    PasswordMismatch,
19    #[error("Sorry, internal error!")]
20    InternalError,
21    #[error("Token generation error!")]
22    TokenError,
23    #[error("secret not supplied! ")]
24    SecretNotFound,
25    #[error("invalid token provided")]
26    InvalidToken,
27    #[error("invalid pin")]
28    PinMisMatch,
29}
30
31#[derive(Debug)]
32pub enum TimeError {
33    TimeGenError,
34}
35
36#[derive(Debug)]
37pub enum CoreError {
38    ParseError,
39}
40
41#[cfg(feature = "actix")]
42#[derive(Serialize)]
43struct ErrorResponse {
44    message: String,
45}
46
47#[cfg(feature = "actix")]
48impl ResponseError for AuthError {
49    fn status_code(&self) -> StatusCode {
50        match self {
51            AuthError::InvalidInput => StatusCode::UNAUTHORIZED,
52            AuthError::StrategyNotFound => StatusCode::INTERNAL_SERVER_ERROR,
53            AuthError::SecretNotFound => StatusCode::INTERNAL_SERVER_ERROR,
54            AuthError::TokenError | AuthError::InvalidToken => StatusCode::UNAUTHORIZED,
55            _ => StatusCode::BAD_REQUEST,
56        }
57    }
58
59    fn error_response(&self) -> HttpResponse {
60        HttpResponse::build(self.status_code()).json(ErrorResponse {
61            message: self.to_string(),
62        })
63    }
64}