hyper_fast/server/
error.rs

1use thiserror::Error;
2
3use crate::server::{HttpResponse, HttpResult};
4
5#[derive(Error, Debug)]
6pub enum ApiError {
7    #[error("Internal Server Error: {0}")]
8    InternalServerError(anyhow::Error),
9
10    #[error("Not Found Error: {0}")]
11    NotFound(String),
12
13    #[error("Forbidden Error: {0}")]
14    Forbidden(String),
15
16    #[error("Bad Request Error: {0}")]
17    BadRequest(#[from] anyhow::Error),
18
19    #[error("Not Content: {0}")]
20    NoContent(String),
21}
22
23impl Into<HttpResult> for ApiError {
24    fn into(self) -> HttpResult {
25        match self {
26            ApiError::InternalServerError(error) => HttpResponse::internal_server_error(error),
27            ApiError::NotFound(reason) => HttpResponse::not_found(&reason),
28            ApiError::Forbidden(reason) => HttpResponse::forbidden(&reason),
29            ApiError::BadRequest(error) => HttpResponse::bad_request(error),
30            ApiError::NoContent(reason) => HttpResponse::no_content(&reason),
31        }
32    }
33}