Skip to main content

ohttp_gateway/
error.rs

1use axum::{
2    Json,
3    http::StatusCode,
4    response::{IntoResponse, Response},
5};
6use serde_json::json;
7use thiserror::Error;
8
9#[derive(Error, Debug)]
10pub enum GatewayError {
11    #[error("Invalid request: {0}")]
12    InvalidRequest(String),
13
14    #[error("Decryption failed: {0}")]
15    DecryptionError(String),
16
17    #[error("Encryption failed: {0}")]
18    EncryptionError(String),
19
20    #[error("Backend error: {0}")]
21    BackendError(String),
22
23    #[error("Request too large: {0}")]
24    RequestTooLarge(String),
25
26    #[error("Configuration error: {0}")]
27    ConfigurationError(String),
28
29    #[error("Internal error: {0}")]
30    InternalError(String),
31}
32
33impl IntoResponse for GatewayError {
34    fn into_response(self) -> Response {
35        let (status, error_code, message) = match self {
36            GatewayError::InvalidRequest(msg) => (StatusCode::BAD_REQUEST, "invalid_request", msg),
37            GatewayError::DecryptionError(msg) => {
38                (StatusCode::BAD_REQUEST, "decryption_error", msg)
39            }
40            GatewayError::EncryptionError(msg) => {
41                (StatusCode::INTERNAL_SERVER_ERROR, "encryption_error", msg)
42            }
43            GatewayError::BackendError(msg) => (StatusCode::BAD_GATEWAY, "backend_error", msg),
44            GatewayError::RequestTooLarge(msg) => {
45                (StatusCode::PAYLOAD_TOO_LARGE, "request_too_large", msg)
46            }
47            GatewayError::ConfigurationError(msg) => (
48                StatusCode::INTERNAL_SERVER_ERROR,
49                "configuration_error",
50                msg,
51            ),
52            GatewayError::InternalError(msg) => {
53                (StatusCode::INTERNAL_SERVER_ERROR, "internal_error", msg)
54            }
55        };
56
57        let body = Json(json!({
58            "error": {
59                "code": error_code,
60                "message": message
61            }
62        }));
63
64        (status, body).into_response()
65    }
66}