use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use crate::Error;
pub struct ApiError(pub Error);
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let status = match &self.0 {
Error::InvalidInput(_) => StatusCode::BAD_REQUEST,
Error::Storage(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::Encoding(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let body = serde_json::json!({
"status": "error",
"message": self.0.to_string()
});
(status, Json(body)).into_response()
}
}
impl From<Error> for ApiError {
fn from(err: Error) -> Self {
ApiError(err)
}
}
impl From<crate::error::AppendError> for ApiError {
fn from(err: crate::error::AppendError) -> Self {
ApiError(Error::from(err))
}
}
impl From<&str> for ApiError {
fn from(msg: &str) -> Self {
ApiError(Error::InvalidInput(msg.to_string()))
}
}