use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde_json::json;
#[derive(Debug, thiserror::Error)]
pub enum AletheiaHttpError {
#[error("{0}")]
BadRequest(String),
#[error("{0}")]
NotFound(String),
#[error("{0}")]
QueryParse(String),
#[error("{0}")]
Internal(String),
#[error("application state not installed")]
StateMissing,
}
impl AletheiaHttpError {
fn status(&self) -> StatusCode {
match self {
Self::BadRequest(_) | Self::QueryParse(_) => StatusCode::BAD_REQUEST,
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::Internal(_) | Self::StateMissing => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl IntoResponse for AletheiaHttpError {
fn into_response(self) -> Response {
let status = self.status();
let body = json!({
"success": false,
"error": self.to_string(),
});
(status, Json(body)).into_response()
}
}