summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: c9fae0f393160696e56a7e14d7b4181ccfeae581 (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
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Axum error: {0}")]
    Axum(#[from] axum::Error),

    #[error("Database error: {0}")]
    Sqlx(#[from] sqlx::Error),

    #[error("Migration error: {0}")]
    Migration(#[from] sqlx::migrate::MigrateError),

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

    #[error("API error: {0}")]
    Api(#[from] crate::api::error::Error),

    #[error("Authorization 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;
        // TODO: implement [rfc7807](https://www.rfc-editor.org/rfc/rfc7807.html)

        match self {
            Self::Api(err) => err.into_response(),
            Self::Auth(err) => err.into_response(),
            err @ Self::RouteNotFound(_) => {
                (StatusCode::NOT_FOUND, err.to_string()).into_response()
            }
            err => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(),
        }
    }
}