File size: 1,108 Bytes
a21c316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 错误处理
use thiserror::Error;
use axum::{http::StatusCode, Json, response::IntoResponse};

#[derive(Debug, Error)]
pub enum ProxyError {
    #[error("Upstream API error: {0}")]
    UpstreamError(String),

    #[error("Transform error: {0}")]
    TransformError(String),

    #[error("Account error: {0}")]
    AccountError(String),

    #[error("Rate limit exceeded")]
    RateLimitExceeded,

    #[error("Invalid request: {0}")]
    InvalidRequest(String),
}

impl IntoResponse for ProxyError {
    fn into_response(self) -> axum::response::Response {
        let status = match &self {
            ProxyError::InvalidRequest(_) => StatusCode::BAD_REQUEST,
            ProxyError::RateLimitExceeded => StatusCode::TOO_MANY_REQUESTS,
            ProxyError::AccountError(_) => StatusCode::UNAUTHORIZED,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

        let body = serde_json::json!({
            "error": {
                "message": self.to_string(),
                "type": format!("{:?}", self)
            }
        });

        (status, Json(body)).into_response()
    }
}