use axum::{
body::Body,
http::{Response, StatusCode},
response::IntoResponse,
};
use flagrant::errors::FlagrantError;
pub struct ServiceError(anyhow::Error);
impl IntoResponse for ServiceError {
fn into_response(self) -> Response<Body> {
match self.0.downcast_ref::<FlagrantError>() {
Some(FlagrantError::UnexpectedFailure(error, cause)) => {
tracing::error!(cause = ?cause, error);
(StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
}
Some(FlagrantError::QueryFailed(error, cause)) => {
tracing::error!(cause = ?cause, error);
(StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
}
Some(FlagrantError::BadRequest(error)) => {
tracing::error!(error);
(StatusCode::BAD_REQUEST, error.to_string())
}
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error: {}", self.0),
),
}
.into_response()
}
}
impl<E> From<E> for ServiceError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}