summaryrefslogtreecommitdiff
path: root/src/errors.rs
blob: c1d6672e86d99237055a841f935c2c4bea6320cf (plain) (blame)
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
use axum::{response::{Response, IntoResponse}, body::{boxed, self}};
use hyper::StatusCode;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ServiceError {
    #[error("Solarlib error: {0}")]
    Solarlib(#[from] solarlib::errors::Error),

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

    #[error("SQL error: {0}")]
    Sql(#[from] sqlx::Error),

    #[error("Not Found")]
    NotFound,
}

pub type StringResult<T = &'static str> = Result<T, ServiceError>;

pub type JsonResult<T> = Result<T, ServiceError>;

impl IntoResponse for ServiceError {
    fn into_response(self) -> Response {
        let body = body::boxed(body::Full::from(self.to_string()));

        let status = match self {
            ServiceError::NotFound => StatusCode::NOT_FOUND,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

        Response::builder()
            .status(status)
            .body(body)
            .unwrap()
    }
}