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, gproxy_protocol_macros::WireBuilder)]
6#[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 extra: JsonObject,
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(
42            Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
43        )]
44        #[non_exhaustive]
45        pub struct $name {
46            pub message: String,
47            #[serde(rename = "type")]
48            pub type_: $type_name,
49            #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
50            pub extra: JsonObject,
51        }
52
53        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54        #[non_exhaustive]
55        pub enum $type_name {
56            #[serde(rename = $wire)]
57            $variant,
58        }
59    };
60}
61
62api_error!(
63    InvalidRequestError,
64    InvalidRequestErrorType,
65    InvalidRequestError,
66    "invalid_request_error"
67);
68
69api_error!(
70    AuthenticationError,
71    AuthenticationErrorType,
72    AuthenticationError,
73    "authentication_error"
74);
75
76api_error!(
77    BillingError,
78    BillingErrorType,
79    BillingError,
80    "billing_error"
81);
82
83api_error!(
84    PermissionError,
85    PermissionErrorType,
86    PermissionError,
87    "permission_error"
88);
89
90api_error!(
91    NotFoundError,
92    NotFoundErrorType,
93    NotFoundError,
94    "not_found_error"
95);
96
97api_error!(
98    RateLimitError,
99    RateLimitErrorType,
100    RateLimitError,
101    "rate_limit_error"
102);
103
104api_error!(
105    GatewayTimeoutError,
106    GatewayTimeoutErrorType,
107    GatewayTimeoutError,
108    "timeout_error"
109);
110
111api_error!(ApiError, ApiErrorType, ApiError, "api_error");
112
113api_error!(
114    OverloadedError,
115    OverloadedErrorType,
116    OverloadedError,
117    "overloaded_error"
118);
119
120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
121#[non_exhaustive]
122pub struct UnknownError {
123    pub message: String,
124    #[serde(rename = "type")]
125    pub type_: String,
126    #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
127    pub extra: JsonObject,
128}