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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/// Generate standard provider error helper functions.
///
/// Creates 9 free functions for a provider: `{prefix}_config_error`, `{prefix}_auth_error`,
/// `{prefix}_api_error`, `{prefix}_network_error`, `{prefix}_parse_error`,
/// `{prefix}_stream_error`, `{prefix}_rate_limit_error`, `{prefix}_model_error`,
/// `{prefix}_validation_error`.
///
/// # Usage
/// ```ignore
/// define_provider_error_helpers!("gemini", gemini);
/// // Generates: gemini_config_error, gemini_auth_error, etc.
/// ```
///
/// Provider-specific helpers (e.g. `gemini_safety_error`) should be defined manually.
#[macro_export]
macro_rules! define_provider_error_helpers {
($provider:expr, $prefix:ident) => {
::pastey::paste! {
/// Create configuration error
pub fn [<$prefix _config_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::configuration($provider, msg.into())
}
/// Create authentication error
pub fn [<$prefix _auth_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::authentication($provider, msg.into())
}
/// Create API error with status code
pub fn [<$prefix _api_error>](status: u16, msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::api_error($provider, status, msg.into())
}
/// Create network error
pub fn [<$prefix _network_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::network($provider, msg.into())
}
/// Create parsing/serialization error
pub fn [<$prefix _parse_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::serialization($provider, msg.into())
}
/// Create streaming error
pub fn [<$prefix _stream_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::streaming_error($provider, "chat", None, None, msg.into())
}
/// Create rate limit error
pub fn [<$prefix _rate_limit_error>](retry_after: Option<u64>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::rate_limit($provider, retry_after)
}
/// Create model not found error
pub fn [<$prefix _model_error>](model: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::model_not_found($provider, model.into())
}
/// Create validation/invalid request error
pub fn [<$prefix _validation_error>](msg: impl Into<String>) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::ProviderError::invalid_request($provider, msg.into())
}
}
};
}
// Legacy methods and ProviderErrorTrait implementation are in provider_error_conversions.rs
/// Generate standard provider error methods on `ProviderError`.
///
/// Creates methods like `{prefix}_authentication`, `{prefix}_rate_limit`, etc.
/// as `impl ProviderError` associated functions.
///
/// # Usage
/// ```ignore
/// impl_provider_error_helpers!("huggingface", huggingface);
/// // Generates: ProviderError::huggingface_authentication(...), etc.
/// ```
///
/// Provider-specific methods should be defined in a separate `impl` block.
#[macro_export]
macro_rules! impl_provider_error_helpers {
($provider:expr, $prefix:ident) => {
::pastey::paste! {
impl $crate::core::providers::unified_provider::ProviderError {
/// Create authentication error
pub fn [<$prefix _authentication>](message: impl Into<String>) -> Self {
Self::authentication($provider, message)
}
/// Create rate limit error
pub fn [<$prefix _rate_limit>](retry_after: Option<u64>) -> Self {
Self::rate_limit($provider, retry_after)
}
/// Create model not found error
pub fn [<$prefix _model_not_found>](model: impl Into<String>) -> Self {
Self::model_not_found($provider, model)
}
/// Create invalid request error
pub fn [<$prefix _invalid_request>](message: impl Into<String>) -> Self {
Self::invalid_request($provider, message)
}
/// Create network error
pub fn [<$prefix _network_error>](message: impl Into<String>) -> Self {
Self::network($provider, message)
}
/// Create timeout error
pub fn [<$prefix _timeout>](message: impl Into<String>) -> Self {
Self::Timeout {
provider: $provider,
message: message.into(),
}
}
/// Create response parsing error
pub fn [<$prefix _response_parsing>](message: impl Into<String>) -> Self {
Self::response_parsing($provider, message)
}
/// Create configuration error
pub fn [<$prefix _configuration>](message: impl Into<String>) -> Self {
Self::configuration($provider, message)
}
/// Create API error with status code
pub fn [<$prefix _api_error>](status: u16, message: impl Into<String>) -> Self {
Self::ApiError {
provider: $provider,
status,
message: message.into(),
}
}
/// Check if this is a provider-specific error
pub fn [<is_ $prefix _error>](&self) -> bool {
self.provider() == $provider
}
}
}
};
}
/// Generate a standard `ErrorMapper` implementation for a provider.
///
/// Creates:
/// - `{Name}ErrorMapper` struct
/// - `ErrorMapper<ProviderError>` impl delegating to `default_http_error_mapper`
///
/// # Usage
/// ```ignore
/// define_standard_error_mapper!("deepseek", DeepSeek);
/// // Generates: pub struct DeepSeekErrorMapper; + impl ErrorMapper<ProviderError>
/// ```
#[macro_export]
macro_rules! define_standard_error_mapper {
($provider:expr, $name:ident) => {
::pastey::paste! {
/// Error mapper for the provider, using the standard HTTP status code mapping.
#[derive(Debug)]
pub struct [<$name ErrorMapper>];
impl $crate::core::traits::error_mapper::trait_def::ErrorMapper<
$crate::core::providers::unified_provider::ProviderError,
> for [<$name ErrorMapper>]
{
fn map_http_error(
&self,
status_code: u16,
response_body: &str,
) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::default_http_error_mapper(
$provider,
status_code,
response_body,
)
}
}
}
};
}
/// Generate an extended `ErrorMapper` implementation for a provider.
///
/// Like `define_standard_error_mapper!` but includes 402, 413, 408/504, 502/503.
#[macro_export]
macro_rules! define_extended_error_mapper {
($provider:expr, $name:ident) => {
::pastey::paste! {
/// Error mapper for the provider, using the extended HTTP status code mapping.
#[derive(Debug)]
pub struct [<$name ErrorMapper>];
impl $crate::core::traits::error_mapper::trait_def::ErrorMapper<
$crate::core::providers::unified_provider::ProviderError,
> for [<$name ErrorMapper>]
{
fn map_http_error(
&self,
status_code: u16,
response_body: &str,
) -> $crate::core::providers::unified_provider::ProviderError {
$crate::core::providers::unified_provider::extended_http_error_mapper(
$provider,
status_code,
response_body,
)
}
}
}
};
}