32 lines
1 KiB
Rust
32 lines
1 KiB
Rust
use actix_web::{HttpResponse, ResponseError, cookie::time::error, http::StatusCode};
|
|
use thiserror::Error;
|
|
|
|
#[derive(Error, Debug)]
|
|
pub enum ApiError {
|
|
#[error("Database Error: {0}")]
|
|
Database(#[from] sea_orm::DbErr),
|
|
#[error("Unauthorized")]
|
|
Unauthorized,
|
|
#[error("Not Found")]
|
|
NotFound,
|
|
#[error("Bad Request: {0}")]
|
|
BadRequest(String),
|
|
#[error("Validation Error: {0}")]
|
|
ValidationError(#[from] validator::ValidationErrors),
|
|
}
|
|
|
|
impl ResponseError for ApiError {
|
|
fn status_code(&self) -> StatusCode {
|
|
match self {
|
|
ApiError::Database(..) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
ApiError::NotFound => StatusCode::NOT_FOUND,
|
|
ApiError::Unauthorized => StatusCode::UNAUTHORIZED,
|
|
ApiError::BadRequest(..) => StatusCode::BAD_REQUEST,
|
|
ApiError::ValidationError(..) => StatusCode::BAD_REQUEST,
|
|
}
|
|
}
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
HttpResponse::build(self.status_code()).body(self.to_string())
|
|
}
|
|
}
|