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

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

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

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

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

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

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

        let status = match self {
            Self::UserNotFound => StatusCode::NOT_FOUND,
            Self::EmailExists => StatusCode::CONFLICT,
            Self::EmailInvalid(_) => StatusCode::UNPROCESSABLE_ENTITY,
            Self::InvalidToken => StatusCode::UNAUTHORIZED,
            Self::Sqlx(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::Auth(err) => return err.into_response(),
        };

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