Skip to main content

gproxy_protocol/claude/common/
errors.rs

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