aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 2d0f911c24adeb9bbd4cb1a7169272f189150ab5 (plain)
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
84
85
86
87
88
89
use axum::extract::rejection::JsonRejection;
use axum::response::{IntoResponse, Response};
use axum_extra::typed_header::TypedHeaderRejection;

use crate::response::{ErrorResponse, FailResponse, SuccessResponse};

pub type ApiResult<T> = Result<SuccessResponse<T>, ApiError>;

pub enum ApiError {
    // 400
    BadJsonBody(String),
    BadAuthTokenHeader(String),
    UserAlreadyExists { username: String },
    InvalidPassword,
    NotAuthorized,
    // 500
    Database(String),
    PasswordHash(String),
    InternalJwt(String),
}

impl From<JsonRejection> for ApiError {
    fn from(value: JsonRejection) -> Self {
        Self::BadJsonBody(value.body_text())
    }
}

impl From<TypedHeaderRejection> for ApiError {
    fn from(value: TypedHeaderRejection) -> Self {
        Self::BadAuthTokenHeader(value.to_string())
    }
}

impl From<sea_orm::DbErr> for ApiError {
    fn from(value: sea_orm::DbErr) -> Self {
        Self::Database(value.to_string())
    }
}

impl From<argon2::password_hash::Error> for ApiError {
    fn from(value: argon2::password_hash::Error) -> Self {
        Self::PasswordHash(value.to_string())
    }
}

impl ToString for ApiError {
    fn to_string(&self) -> String {
        match self {
            // 400
            ApiError::BadJsonBody(..) => "BadJsonBody",
            ApiError::BadAuthTokenHeader(..) => "BadAuthTokenHeader",
            ApiError::UserAlreadyExists { .. } => "UserAlreadyExists",
            ApiError::InvalidPassword => "InvalidPassword",
            ApiError::NotAuthorized => "NotAuthorized",
            // 500
            ApiError::Database(..) => "Database",
            ApiError::PasswordHash(..) => "PasswordHash",
            ApiError::InternalJwt(..) => "InternalJwt",
        }
        .to_string()
    }
}

impl IntoResponse for ApiError {
    fn into_response(self) -> Response {
        let kind = self.to_string();
        match self {
            // 400
            ApiError::BadJsonBody(msg) => FailResponse(kind, msg).into_response(),
            ApiError::BadAuthTokenHeader(msg) => FailResponse(kind, msg).into_response(),
            ApiError::UserAlreadyExists { username } => FailResponse(
                kind,
                format!("user with username `{}` already exists", username),
            )
            .into_response(),
            ApiError::InvalidPassword => {
                FailResponse(kind, "password is invalid".to_string()).into_response()
            }
            ApiError::NotAuthorized => {
                FailResponse(kind, "user is not authorized".to_string()).into_response()
            }

            // 500
            ApiError::Database(msg) => ErrorResponse(kind, msg).into_response(),
            ApiError::PasswordHash(msg) => ErrorResponse(kind, msg).into_response(),
            ApiError::InternalJwt(msg) => ErrorResponse(kind, msg).into_response(),
        }
    }
}