use actix_web::{HttpResponse, ResponseError};
use thiserror::Error;
use crate::api::response::{
bad_gateway, bad_request, conflict, forbidden, internal_error, not_found, service_unavailable,
unauthorized,
};
pub type ApiResult<T> = Result<T, ApiError>;
#[derive(Debug, Error)]
pub enum ApiError {
#[error("{0}")]
Validation(String),
#[error("{0}")]
NotFound(String),
#[error("{0}")]
Unauthorized(String),
#[error("{0}")]
Forbidden(String),
#[error("{0}")]
Conflict(String),
#[error("{0}")]
Backend(String),
#[error("{0}")]
ServiceUnavailable(String),
#[error("{0}")]
BadGateway(String),
}
impl ResponseError for ApiError {
fn error_response(&self) -> HttpResponse {
match self {
ApiError::Validation(msg) => bad_request("Bad request", msg),
ApiError::NotFound(msg) => not_found("Not found", msg),
ApiError::Unauthorized(msg) => unauthorized("Unauthorized", msg),
ApiError::Forbidden(msg) => forbidden("Forbidden", msg),
ApiError::Conflict(msg) => conflict("Conflict", msg),
ApiError::Backend(msg) => internal_error("Backend error", msg),
ApiError::ServiceUnavailable(msg) => service_unavailable("Service unavailable", msg),
ApiError::BadGateway(msg) => bad_gateway("Bad gateway", msg),
}
}
}
impl From<String> for ApiError {
fn from(s: String) -> Self {
ApiError::Validation(s)
}
}
impl From<&str> for ApiError {
fn from(s: &str) -> Self {
ApiError::Validation(s.to_string())
}
}