use axum::http::StatusCode; use axum::response::IntoResponse; use axum::response::Response; use crate::errors::{AcudoError, EventError, HttpError}; /// Web layer error wrapper for HTTP responses. /// /// This type wraps the standardized HttpError and provides HTTP response conversion. /// All errors follow the format: `error-acudo-http- :
` #[derive(Debug)] pub(super) enum WebError { /// HTTP layer error with proper error formatting Http(HttpError), /// Fallback for legacy anyhow errors during transition Legacy(anyhow::Error), } impl From for WebError { fn from(err: HttpError) -> Self { WebError::Http(err) } } impl From for WebError { fn from(err: AcudoError) -> Self { match err { AcudoError::Http(http_err) => WebError::Http(http_err), other => WebError::Legacy(anyhow::Error::from(other)), } } } impl From for WebError { fn from(err: EventError) -> Self { WebError::Legacy(anyhow::Error::from(err)) } } impl From for WebError { fn from(err: anyhow::Error) -> Self { WebError::Legacy(err) } } /// Implementation of Axum's `IntoResponse` trait for WebError. /// /// This implementation converts errors into appropriate HTTP responses: /// - Authentication errors return 401 Unauthorized /// - Forbidden errors return 403 Forbidden /// - Not found errors return 404 Not Found /// - Validation errors return 400 Bad Request /// - All other errors return 500 Internal Server Error impl IntoResponse for WebError { fn into_response(self) -> Response { let (status, error_message) = match &self { WebError::Http(http_err) => match http_err { HttpError::AuthenticationRequired => { (StatusCode::UNAUTHORIZED, http_err.to_string()) } HttpError::Forbidden => (StatusCode::FORBIDDEN, http_err.to_string()), HttpError::NotFound => (StatusCode::NOT_FOUND, http_err.to_string()), HttpError::RequestValidation { .. } => { (StatusCode::BAD_REQUEST, http_err.to_string()) } HttpError::Unhandled { .. } => { (StatusCode::INTERNAL_SERVER_ERROR, http_err.to_string()) } }, WebError::Legacy(err) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("error-acudo-http-100 Unhandled web error: {}", err), ), }; tracing::error!(error = ?self, status = ?status, "HTTP error response"); // Return simple error response for now // In production, you might want to return JSON or a proper error page (status, error_message).into_response() } }