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), } pub type StringResult = Result; pub type HtmlResult> = Result; pub type JsonResult = Result; 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() } }