use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ProxyError {
#[error("Failed to create HTTP client: {0}")]
ClientCreation(String),
#[error("Failed to read request body: {0}")]
RequestBody(String),
#[error("Backend request failed: {0}")]
BackendRequest(String),
#[error("Failed to read response body: {0}")]
ResponseBody(String),
#[error("Route not found for: {0}")]
RouteNotFound(String),
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
}
impl IntoResponse for ProxyError {
fn into_response(self) -> Response {
let (status, error_message) = match &self {
ProxyError::RouteNotFound(_) => (StatusCode::NOT_FOUND, self.to_string()),
ProxyError::InvalidConfig(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
ProxyError::BackendRequest(_) => (StatusCode::BAD_GATEWAY, self.to_string()),
_ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
};
#[derive(Serialize)]
struct ErrorResponse {
error: String,
message: String,
}
let body = Json(ErrorResponse {
error: "PROXY_ERROR".to_string(),
message: error_message,
});
(status, body).into_response()
}
}
pub type ProxyResult<T> = Result<T, ProxyError>;