use axum::response::IntoResponse;
use http::StatusCode;
use sea_orm::DbErr;
use crate::http::response::response;
impl IntoResponse for crate::error::Error {
fn into_response(self) -> axum::response::Response {
let (status, message) = match self {
Self::IO(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()),
Self::DB(error) => {
let status = match error {
DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(status, error.to_string())
}
Self::SerdeJson(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()),
Self::Token(error) => (StatusCode::UNAUTHORIZED, error.to_string()),
Self::Validate(error) => (
StatusCode::BAD_REQUEST,
format!("Validation error: {}", error),
),
Self::FormRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::PathRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::QueryRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::JsonRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::MultipartRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::Multipart(error) => (StatusCode::BAD_REQUEST, error.to_string()),
Self::InvalidFilePath => (StatusCode::BAD_REQUEST, self.to_string()),
Self::Anyhow(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", error),
),
_ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
};
tracing::error!("{}", message);
response().code(status).message(message).json()
}
}