1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use thiserror::Error;

use crate::server::{HttpResponse, HttpResult};

#[derive(Error, Debug)]
pub enum ApiError {
    #[error("Internal Server Error: {0}")]
    InternalServerError(anyhow::Error),

    #[error("Not Found Error: {0}")]
    NotFound(String),

    #[error("Forbidden Error: {0}")]
    Forbidden(String),

    #[error("Bad Request Error: {0}")]
    BadRequest(#[from] anyhow::Error),

    #[error("Not Content: {0}")]
    NoContent(String),
}

impl Into<HttpResult> for ApiError {
    fn into(self) -> HttpResult {
        match self {
            ApiError::InternalServerError(error) => HttpResponse::internal_server_error(error),
            ApiError::NotFound(reason) => HttpResponse::not_found(&reason),
            ApiError::Forbidden(reason) => HttpResponse::forbidden(&reason),
            ApiError::BadRequest(error) => HttpResponse::bad_request(error),
            ApiError::NoContent(reason) => HttpResponse::no_content(&reason),
        }
    }
}