Skip to main content

gproxy_protocol/protocol/claude/common/
errors.rs

1use serde::{Deserialize, Serialize};
2
3use super::JsonObject;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct ErrorResponse {
7    #[serde(rename = "type")]
8    pub type_: ErrorResponseType,
9    pub error: ErrorBody,
10    pub request_id: String,
11    #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
12    pub extra: JsonObject,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub enum ErrorResponseType {
17    #[serde(rename = "error")]
18    Error,
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(untagged)]
23pub enum ErrorBody {
24    InvalidRequest(InvalidRequestError),
25    Authentication(AuthenticationError),
26    Billing(BillingError),
27    Permission(PermissionError),
28    NotFound(NotFoundError),
29    RateLimit(RateLimitError),
30    GatewayTimeout(GatewayTimeoutError),
31    Api(ApiError),
32    Overloaded(OverloadedError),
33    Unknown(UnknownError),
34}
35
36macro_rules! api_error {
37    ($name:ident, $type_name:ident, $variant:ident, $wire:literal) => {
38        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
39        pub struct $name {
40            pub message: String,
41            #[serde(rename = "type")]
42            pub type_: $type_name,
43            #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
44            pub extra: JsonObject,
45        }
46
47        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
48        pub enum $type_name {
49            #[serde(rename = $wire)]
50            $variant,
51        }
52    };
53}
54
55api_error!(
56    InvalidRequestError,
57    InvalidRequestErrorType,
58    InvalidRequestError,
59    "invalid_request_error"
60);
61
62api_error!(
63    AuthenticationError,
64    AuthenticationErrorType,
65    AuthenticationError,
66    "authentication_error"
67);
68
69api_error!(
70    BillingError,
71    BillingErrorType,
72    BillingError,
73    "billing_error"
74);
75
76api_error!(
77    PermissionError,
78    PermissionErrorType,
79    PermissionError,
80    "permission_error"
81);
82
83api_error!(
84    NotFoundError,
85    NotFoundErrorType,
86    NotFoundError,
87    "not_found_error"
88);
89
90api_error!(
91    RateLimitError,
92    RateLimitErrorType,
93    RateLimitError,
94    "rate_limit_error"
95);
96
97api_error!(
98    GatewayTimeoutError,
99    GatewayTimeoutErrorType,
100    GatewayTimeoutError,
101    "timeout_error"
102);
103
104api_error!(ApiError, ApiErrorType, ApiError, "api_error");
105
106api_error!(
107    OverloadedError,
108    OverloadedErrorType,
109    OverloadedError,
110    "overloaded_error"
111);
112
113#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
114pub struct UnknownError {
115    pub message: String,
116    #[serde(rename = "type")]
117    pub type_: String,
118    #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
119    pub extra: JsonObject,
120}