File size: 2,642 Bytes
f4d1c72
d94c6df
 
 
 
f4d1c72
 
d94c6df
f4d1c72
 
d94c6df
f4d1c72
 
d94c6df
f4d1c72
 
d94c6df
f4d1c72
 
 
 
 
d94c6df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4d1c72
 
d94c6df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4d1c72
 
 
 
 
 
 
 
d94c6df
f4d1c72
 
 
 
 
 
d94c6df
f4d1c72
d94c6df
 
 
 
 
 
 
 
f4d1c72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use actix_web::{HttpResponse, ResponseError};
use thiserror::Error;

#[derive(Debug, Error)]
pub(crate) enum AppError {
    #[error("`{0}`")]
    User(#[from] UserError),

    #[error("`{0}`")]
    Json(#[from] serde_json::Error),

    #[error("`{0}`")]
    Actix(#[from] actix_web::Error),

    #[error("`{0}`")]
    Db(#[from] sea_orm::DbErr),

    #[error("`{0}`")]
    MinioS3(#[from] minio::s3::error::Error),

    #[error("`{0}`")]
    Std(#[from] std::io::Error),
}

#[derive(Debug, Error)]
pub(crate) enum UserError {
    #[error("`username` field of `User` cannot be empty!")]
    EmptyUsername,

    #[error("`username` field of `User` cannot contain whitespaces!")]
    UsernameInvalidCharacter,

    #[error("`password` field of `User` cannot be empty!")]
    EmptyPassword,

    #[error("`password` field of `User` cannot contain whitespaces!")]
    PasswordInvalidCharacter,

    #[error("Could not find any `User` for id: `{0}`!")]
    NotFound(i64),

    #[error("Failed to login user!")]
    LoginFailed,

    #[error("User is not logged in!")]
    NotLoggedIn,

    #[error("Invalid authorization token!")]
    InvalidToken,

    #[error("Could not find any `User`!")]
    Empty,
}

impl ResponseError for AppError {
    fn status_code(&self) -> actix_web::http::StatusCode {
        match self {
            AppError::User(user_error) => match user_error {
                UserError::EmptyUsername => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
                UserError::UsernameInvalidCharacter => {
                    actix_web::http::StatusCode::UNPROCESSABLE_ENTITY
                }
                UserError::EmptyPassword => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
                UserError::PasswordInvalidCharacter => {
                    actix_web::http::StatusCode::UNPROCESSABLE_ENTITY
                }
                UserError::NotFound(_) => actix_web::http::StatusCode::NOT_FOUND,
                UserError::NotLoggedIn => actix_web::http::StatusCode::UNAUTHORIZED,
                UserError::Empty => actix_web::http::StatusCode::NOT_FOUND,
                UserError::LoginFailed => actix_web::http::StatusCode::NOT_FOUND,
                UserError::InvalidToken => actix_web::http::StatusCode::UNAUTHORIZED,
            },
            AppError::Actix(fail) => fail.as_response_error().status_code(),
            _ => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn error_response(&self) -> HttpResponse {
        let status_code = self.status_code();
        let response = HttpResponse::build(status_code).body(self.to_string());
        response
    }
}