diff options
Diffstat (limited to 'src/errors.rs')
-rw-r--r-- | src/errors.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..19e0a8a --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,35 @@ +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) +} + +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() + } +} |