aboutsummaryrefslogblamecommitdiff
path: root/src/errors.rs
blob: e0a6df99ebe864235004cd18e4afb9f61e0d4155 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                             
                                   
                                              


                               


                         





                                    




                                                                               
                                                            




                                                                   




                                                            
                           
                           



                       
use thiserror::Error;

use axum::response::{Response, IntoResponse};
use axum::http::StatusCode;
use axum::body;
use axum::Json;

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

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

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

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

    #[error("Command error: {0}")]
    Command(#[from] std::io::Error)
}

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

pub type JsonResult<T> = std::result::Result<T, ServiceError>;
pub type NoneResult = std::result::Result<(), 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()
    }
}