summaryrefslogblamecommitdiff
path: root/src/errors.rs
blob: e513343d0d16bd10a9c4393ef49b9181e0d6ef84 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14



                                       









                                              


                              
 


                                                

                         


                              

 


                                                              


                                                 

                                               



                                                                   

                                                            
                                                                    


                                                   
                                                              

     
use axum::{
    body::{self, boxed},
    response::{IntoResponse, Response},
};
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("Generic: {0}")]
    Generic(#[from] Box<dyn std::error::Error>),

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

    #[error("Not Authorized")]
    NotAuthorized,
}

pub type StringResult<T = String> = Result<T, ServiceError>;

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

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

pub type NoneResult = 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,
            ServiceError::NotAuthorized => StatusCode::UNAUTHORIZED,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

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