blob: dcc0ae8f4dd8c268e509937aa33bf99d0bc8d62c (
plain) (
tree)
|
|
use axum::body;
use axum::response::{IntoResponse, Response, Html};
use hyper::StatusCode;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ServiceError {
#[error("No config file")]
MissingConfig,
#[error("Invalid config file: {0}")]
InvalidConfig(#[from] toml::de::Error),
#[error("Not Authorized")]
NotAuthorized,
#[error("Not Found")]
NotFound,
#[error("Axum: {0}")]
Axum(#[from] axum::Error),
#[error("SQL: {0}")]
Sql(#[from] sqlx::Error),
#[error("Bcrypt: {0}")]
Bcrypt(#[from] bcrypt::BcryptError),
#[error("Parse Error: {0}")]
Parse(String),
#[error("Email: {0}")]
Email(String),
}
pub type StringResult<T = String> = Result<T, ServiceError>;
pub type HtmlResult<T = Html<String>> = Result<T, ServiceError>;
pub type JsonResult<T> = Result<T, ServiceError>;
impl IntoResponse for ServiceError {
fn into_response(self) -> axum::response::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()
}
}
impl From<ipnetwork::IpNetworkError> for ServiceError {
fn from(e: ipnetwork::IpNetworkError) -> Self {
Self::Parse(e.to_string())
}
}
|