summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 109c944a6030be37ae0e3a4187d50f70274a9060 (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(Debug, thiserror::Error)]
pub enum Error {
    #[error("IO error: {0}")]
    IO(#[from] std::io::Error),

    #[error("Config error: {0}")]
    Toml(#[from] toml::de::Error),

    #[error("Request error: {0}")]
    Reqwest(#[from] reqwest::Error),

    #[error("Invalid HTTP method")]
    Method,

    #[error("Axum error: {0}")]
    Axum(#[from] axum::Error),

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

    #[error("Service not found: {0}")]
    ServiceNotFound(String),
}

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

        let status = match self {
            Self::RouteNotFound(_) | Self::ServiceNotFound(_) => StatusCode::NOT_FOUND,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

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