use super::*;
#[derive(Debug)]
pub struct ApiFailure {
pub status: StatusCode,
pub message: String,
}
impl ApiFailure {
pub fn new(status: StatusCode, message: impl Into<String>) -> Self {
Self {
status,
message: message.into(),
}
}
pub fn bad_request(message: impl Into<String>) -> Self {
Self::new(StatusCode::BAD_REQUEST, message)
}
pub fn conflict(message: impl Into<String>) -> Self {
Self::new(StatusCode::CONFLICT, message)
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(StatusCode::NOT_FOUND, message)
}
pub fn unavailable(message: impl Into<String>) -> Self {
Self::new(StatusCode::SERVICE_UNAVAILABLE, message)
}
}
impl std::fmt::Display for ApiFailure {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}: {}", self.status, self.message)
}
}
impl From<ApiError> for ApiFailure {
fn from(error: ApiError) -> Self {
Self::new(error.status, error.message)
}
}
impl From<anyhow::Error> for ApiFailure {
fn from(error: anyhow::Error) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, format!("{error:#}"))
}
}
#[derive(Debug, Serialize)]
pub(super) struct FailureBody {
pub(super) error: String,
}
impl IntoResponse for ApiFailure {
fn into_response(self) -> Response {
(
self.status,
Json(FailureBody {
error: self.message,
}),
)
.into_response()
}
}