summaryrefslogtreecommitdiffstats
path: root/src/api/error.rs
blob: bc75d41a4f647503042a45fec8c312b2d28996ce (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
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Database error: {0}")]
    Sqlx(#[source] sqlx::Error),

    #[error("Route not found: {0}")]
    RouteNotFound(axum::http::Uri),

    #[error("User not found")]
    UserNotFound,

    #[error("Task not found")]
    TaskNotFound,

    #[error("Required header not found: {0}")]
    HeaderNotFound(axum::http::HeaderName),

    #[error("Failed to parse header: {0} (wrong token type?)")]
    Header(axum_extra::typed_header::TypedHeaderRejection),

    #[error("Failed to parse header: {0}")]
    HeaderValue(#[from] axum::http::header::InvalidHeaderValue),

    #[error("Invalid user token")]
    InvalidToken,

    #[error("User with that email already exists")]
    UserExists,

    #[error("Invalid email: {0}")]
    EmailInvalid(#[from] email_address::Error),

    #[error("Failed to reach authentication server: {0}")]
    AuthRequest(#[from] axum::http::Error),

    #[error("Authentication error: {0}")]
    Auth(#[from] crate::auth::error::Error),
}

impl From<sqlx::Error> for Error {
    fn from(value: sqlx::Error) -> Self {
        match value {
            sqlx::Error::Database(db_err)
                if db_err.is_unique_violation() && db_err.table().is_some_and(|s| s == "user_") =>
            {
                Error::UserExists
            }
            err => Error::Sqlx(err),
        }
    }
}

impl From<axum_extra::typed_header::TypedHeaderRejection> for Error {
    fn from(value: axum_extra::typed_header::TypedHeaderRejection) -> Self {
        if value.is_missing() {
            Self::HeaderNotFound(value.name().clone())
        } else {
            Self::Header(value)
        }
    }
}

impl axum::response::IntoResponse for Error {
    fn into_response(self) -> axum::response::Response {
        use axum::http::header::AUTHORIZATION;
        use axum::http::StatusCode;

        let status = match self {
            Self::RouteNotFound(_) | Self::UserNotFound | Self::TaskNotFound => {
                StatusCode::NOT_FOUND
            }
            Self::UserExists => StatusCode::CONFLICT,
            Self::InvalidToken => StatusCode::UNAUTHORIZED,
            Self::HeaderNotFound(ref h) if h == AUTHORIZATION => StatusCode::UNAUTHORIZED,
            Self::HeaderNotFound(_) => StatusCode::BAD_REQUEST,
            Self::EmailInvalid(_) | Self::Header(_) => StatusCode::UNPROCESSABLE_ENTITY,
            Self::AuthRequest(_) | Self::Sqlx(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::HeaderValue(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::Auth(err) => return err.into_response(),
        };

        (status, self.to_string()).into_response()
    }
}