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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
//! Unified Provider Error Handling
//!
//! Single error type for all providers - optimized design for simplicity and performance
//!
//! This module provides a unified error handling system for all AI providers.
//!
//! ## Core Components
//!
//! ### `ProviderError` Enum
//! A comprehensive error type that covers all possible failure scenarios across different AI providers.
//!
//! | Variant | Purpose | HTTP Status | Retryable |
//! |------|------|------------|--------|
//! | Authentication | Authentication failed | 401 | No |
//! | RateLimit | Rate limit exceeded | 429 | Yes (after delay) |
//! | ModelNotFound | Model not found | 404 | No |
//! | InvalidRequest | Invalid request | 400 | No |
//! | Network | Network error | 500 | Yes |
//! | Timeout | Timeout | 408 | Yes |
//! | Internal | Internal error | 500 | Yes |
//! | ServiceUnavailable | Service unavailable | 503 | Yes |
//! | QuotaExceeded | Quota exceeded | 402 | No |
//! | NotSupported | Feature not supported | 501 | No |
//! | Other | Other error | 500 | No |
//!
//! ## Usage
//!
//! ```rust
//! use litellm_rs::ProviderError;
//!
//! // 1. Direct construction
//! let err = ProviderError::Authentication {
//! provider: "openai",
//! message: "Invalid API key".to_string()
//! };
//!
//! // 2. Use factory methods (preferred)
//! let err = ProviderError::authentication("openai", "Invalid API key");
//! let err = ProviderError::rate_limit("anthropic", Some(60));
//!
//! // 3. Check error properties
//! if err.is_retryable() {
//! if let Some(delay) = err.retry_delay() {
//! println!("Retry after {} seconds", delay);
//! }
//! }
//! ```
//!
//! ## Migration Guide
//!
//! For migrating from provider-specific error types:
//!
//! ```rust
//! // Old code
//! // pub enum MyProviderError { ... }
//!
//! // New code - use unified error type
//! use litellm_rs::ProviderError;
//! pub type MyProviderError = ProviderError;
//! ```
//!
//! ## Design Advantages
//!
//! - **Unified Interface**: Single error type for all providers eliminates conversion overhead
//! - **Rich Context**: Structured error information with provider-specific details
//! - **Retry Logic**: Built-in retry determination and delay calculation
//! - **HTTP Mapping**: Automatic HTTP status code mapping for web APIs
//! - **Performance**: Zero-cost abstractions with compile-time optimization
// Re-export ContextualError from the dedicated module
pub use super::contextual_error::ContextualError;
/// Unified provider error type - single error for all providers
/// This eliminates the need for error type conversion and simplifies the architecture
#[derive(Debug, Clone, thiserror::Error)]
pub enum ProviderError {
#[error("Authentication failed for {provider}: {message}")]
Authentication {
provider: &'static str,
message: String,
},
#[error("Rate limit exceeded for {provider}: {message}")]
RateLimit {
provider: &'static str,
message: String,
retry_after: Option<u64>,
/// Requests per minute limit
rpm_limit: Option<u32>,
/// Tokens per minute limit
tpm_limit: Option<u32>,
/// Current usage level
current_usage: Option<f64>,
},
#[error("Quota exceeded for {provider}: {message}")]
QuotaExceeded {
provider: &'static str,
message: String,
},
#[error("Model '{model}' not found for {provider}")]
ModelNotFound {
provider: &'static str,
model: String,
},
#[error("Invalid request for {provider}: {message}")]
InvalidRequest {
provider: &'static str,
message: String,
},
#[error("Network error for {provider}: {message}")]
Network {
provider: &'static str,
message: String,
},
#[error("Provider {provider} is unavailable: {message}")]
ProviderUnavailable {
provider: &'static str,
message: String,
},
#[error("Feature '{feature}' not supported by {provider}")]
NotSupported {
provider: &'static str,
feature: String,
},
#[error("Feature '{feature}' not implemented for {provider}")]
NotImplemented {
provider: &'static str,
feature: String,
},
#[error("Configuration error for {provider}: {message}")]
Configuration {
provider: &'static str,
message: String,
},
#[error("Serialization error for {provider}: {message}")]
Serialization {
provider: &'static str,
message: String,
},
#[error("Timeout for {provider}: {message}")]
Timeout {
provider: &'static str,
message: String,
},
// Enhanced error variants based on ultrathink analysis
/// Context length exceeded with structured limits (VertexAI pattern)
#[error("Context length exceeded for {provider}: max {max} tokens, got {actual} tokens")]
ContextLengthExceeded {
provider: &'static str,
max: usize,
actual: usize,
},
/// Content filtered by safety systems (VertexAI/OpenAI pattern)
#[error("Content filtered by {provider} safety systems: {reason}")]
ContentFiltered {
provider: &'static str,
reason: String,
/// Policy categories that were violated
policy_violations: Option<Vec<String>>,
/// Whether this might succeed with prompt modification
potentially_retryable: Option<bool>,
},
/// API error with status code (Universal pattern)
#[error("API error for {provider} (status {status}): {message}")]
ApiError {
provider: &'static str,
status: u16,
message: String,
},
/// Token limit exceeded (separate from context length)
#[error("Token limit exceeded for {provider}: {message}")]
TokenLimitExceeded {
provider: &'static str,
message: String,
},
/// Feature disabled by provider (VertexAI pattern)
#[error("Feature disabled for {provider}: {feature}")]
FeatureDisabled {
provider: &'static str,
feature: String,
},
/// Azure deployment specific error
#[error("Azure deployment error for {deployment}: {message}")]
DeploymentError {
provider: &'static str,
deployment: String,
message: String,
},
/// Response parsing error (universal pattern)
#[error("Failed to parse {provider} response: {message}")]
ResponseParsing {
provider: &'static str,
message: String,
},
/// Multi-provider routing error (OpenRouter pattern)
#[error("Routing error from {provider}: tried {attempted_providers:?}, final error: {message}")]
RoutingError {
provider: &'static str,
attempted_providers: Vec<String>,
message: String,
},
/// Transformation error between provider formats (OpenRouter pattern)
#[error("Transformation error for {provider}: from {from_format} to {to_format}: {message}")]
TransformationError {
provider: &'static str,
from_format: String,
to_format: String,
message: String,
},
/// Async operation cancelled (Rust async pattern)
#[error("Operation cancelled for {provider}: {operation_type}")]
Cancelled {
provider: &'static str,
operation_type: String,
/// Reason for cancellation
cancellation_reason: Option<String>,
},
/// Streaming operation error (SSE/WebSocket pattern)
#[error("Streaming error for {provider}: {stream_type} at position {position:?}")]
Streaming {
provider: &'static str,
/// Type of stream (chat, completion, etc.)
stream_type: String,
/// Position in stream where error occurred
position: Option<u64>,
/// Last valid chunk received
last_chunk: Option<String>,
/// Error message
message: String,
},
#[error("{provider} error: {message}")]
Other {
provider: &'static str,
message: String,
},
}
impl ProviderError {
/// Create authentication error
pub fn authentication(provider: &'static str, message: impl Into<String>) -> Self {
Self::Authentication {
provider,
message: message.into(),
}
}
/// Create rate limit error
pub fn rate_limit(provider: &'static str, retry_after: Option<u64>) -> Self {
Self::RateLimit {
provider,
message: match retry_after {
Some(seconds) => format!("Rate limit exceeded. Retry after {} seconds", seconds),
None => "Rate limit exceeded".to_string(),
},
retry_after,
rpm_limit: None,
tpm_limit: None,
current_usage: None,
}
}
/// Create enhanced rate limit error with usage details
pub fn rate_limit_with_limits(
provider: &'static str,
retry_after: Option<u64>,
rpm_limit: Option<u32>,
tpm_limit: Option<u32>,
current_usage: Option<f64>,
) -> Self {
let message = match (rpm_limit, tpm_limit) {
(Some(rpm), Some(tpm)) => {
format!("Rate limit exceeded: {}RPM, {}TPM limits reached", rpm, tpm)
}
(Some(rpm), None) => format!("Rate limit exceeded: {}RPM limit reached", rpm),
(None, Some(tpm)) => format!("Rate limit exceeded: {}TPM limit reached", tpm),
(None, None) => "Rate limit exceeded".to_string(),
};
Self::RateLimit {
provider,
message,
retry_after,
rpm_limit,
tpm_limit,
current_usage,
}
}
/// Create quota exceeded error
pub fn quota_exceeded(provider: &'static str, message: impl Into<String>) -> Self {
Self::QuotaExceeded {
provider,
message: message.into(),
}
}
/// Create simple rate limit error (convenience method)
pub fn rate_limit_simple(provider: &'static str, message: impl Into<String>) -> Self {
Self::RateLimit {
provider,
message: message.into(),
retry_after: None,
rpm_limit: None,
tpm_limit: None,
current_usage: None,
}
}
/// Create rate limit error with retry_after only
pub fn rate_limit_with_retry(
provider: &'static str,
message: impl Into<String>,
retry_after: Option<u64>,
) -> Self {
Self::RateLimit {
provider,
message: message.into(),
retry_after,
rpm_limit: None,
tpm_limit: None,
current_usage: None,
}
}
/// Create model not found error
pub fn model_not_found(provider: &'static str, model: impl Into<String>) -> Self {
Self::ModelNotFound {
provider,
model: model.into(),
}
}
/// Create invalid request error
pub fn invalid_request(provider: &'static str, message: impl Into<String>) -> Self {
Self::InvalidRequest {
provider,
message: message.into(),
}
}
/// Create network error
pub fn network(provider: &'static str, message: impl Into<String>) -> Self {
Self::Network {
provider,
message: message.into(),
}
}
/// Create provider unavailable error
pub fn provider_unavailable(provider: &'static str, message: impl Into<String>) -> Self {
Self::ProviderUnavailable {
provider,
message: message.into(),
}
}
/// Create not supported error
pub fn not_supported(provider: &'static str, feature: impl Into<String>) -> Self {
Self::NotSupported {
provider,
feature: feature.into(),
}
}
/// Create not implemented error
pub fn not_implemented(provider: &'static str, feature: impl Into<String>) -> Self {
Self::NotImplemented {
provider,
feature: feature.into(),
}
}
/// Create configuration error
pub fn configuration(provider: &'static str, message: impl Into<String>) -> Self {
Self::Configuration {
provider,
message: message.into(),
}
}
/// Create serialization error
pub fn serialization(provider: &'static str, message: impl Into<String>) -> Self {
Self::Serialization {
provider,
message: message.into(),
}
}
/// Create timeout error
pub fn timeout(provider: &'static str, message: impl Into<String>) -> Self {
Self::Timeout {
provider,
message: message.into(),
}
}
/// Create initialization error (provider failed to start)
pub fn initialization(provider: &'static str, message: impl Into<String>) -> Self {
Self::Network {
provider,
message: format!("Initialization failed: {}", message.into()),
}
}
// Enhanced factory methods for new error variants
/// Create context length exceeded error with structured data
pub fn context_length_exceeded(provider: &'static str, max: usize, actual: usize) -> Self {
Self::ContextLengthExceeded {
provider,
max,
actual,
}
}
/// Create API error with status code
pub fn api_error(provider: &'static str, status: u16, message: impl Into<String>) -> Self {
Self::ApiError {
provider,
status,
message: message.into(),
}
}
/// Create token limit exceeded error
pub fn token_limit_exceeded(provider: &'static str, message: impl Into<String>) -> Self {
Self::TokenLimitExceeded {
provider,
message: message.into(),
}
}
/// Create feature disabled error
pub fn feature_disabled(provider: &'static str, feature: impl Into<String>) -> Self {
Self::FeatureDisabled {
provider,
feature: feature.into(),
}
}
/// Create Azure deployment error
pub fn deployment_error(deployment: impl Into<String>, message: impl Into<String>) -> Self {
Self::DeploymentError {
provider: "azure",
deployment: deployment.into(),
message: message.into(),
}
}
/// Create response parsing error
pub fn response_parsing(provider: &'static str, message: impl Into<String>) -> Self {
Self::ResponseParsing {
provider,
message: message.into(),
}
}
/// Create routing error
pub fn routing_error(
provider: &'static str,
attempted_providers: Vec<String>,
message: impl Into<String>,
) -> Self {
Self::RoutingError {
provider,
attempted_providers,
message: message.into(),
}
}
/// Create transformation error
pub fn transformation_error(
provider: &'static str,
from_format: impl Into<String>,
to_format: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::TransformationError {
provider,
from_format: from_format.into(),
to_format: to_format.into(),
message: message.into(),
}
}
/// Create content filtered error
pub fn content_filtered(
provider: &'static str,
reason: impl Into<String>,
policy_violations: Option<Vec<String>>,
potentially_retryable: Option<bool>,
) -> Self {
Self::ContentFiltered {
provider,
reason: reason.into(),
policy_violations,
potentially_retryable,
}
}
/// Create cancellation error
pub fn cancelled(
provider: &'static str,
operation_type: impl Into<String>,
cancellation_reason: Option<String>,
) -> Self {
Self::Cancelled {
provider,
operation_type: operation_type.into(),
cancellation_reason,
}
}
/// Create streaming error
pub fn streaming_error(
provider: &'static str,
stream_type: impl Into<String>,
position: Option<u64>,
last_chunk: Option<String>,
message: impl Into<String>,
) -> Self {
Self::Streaming {
provider,
stream_type: stream_type.into(),
position,
last_chunk,
message: message.into(),
}
}
/// Create other/generic error
pub fn other(provider: &'static str, message: impl Into<String>) -> Self {
Self::Other {
provider,
message: message.into(),
}
}
/// Get the provider name that caused this error
pub fn provider(&self) -> &'static str {
match self {
Self::Authentication { provider, .. }
| Self::RateLimit { provider, .. }
| Self::QuotaExceeded { provider, .. }
| Self::ModelNotFound { provider, .. }
| Self::InvalidRequest { provider, .. }
| Self::Network { provider, .. }
| Self::ProviderUnavailable { provider, .. }
| Self::NotSupported { provider, .. }
| Self::NotImplemented { provider, .. }
| Self::Configuration { provider, .. }
| Self::Serialization { provider, .. }
| Self::Timeout { provider, .. }
| Self::ContextLengthExceeded { provider, .. }
| Self::ContentFiltered { provider, .. }
| Self::ApiError { provider, .. }
| Self::TokenLimitExceeded { provider, .. }
| Self::FeatureDisabled { provider, .. }
| Self::DeploymentError { provider, .. }
| Self::ResponseParsing { provider, .. }
| Self::RoutingError { provider, .. }
| Self::TransformationError { provider, .. }
| Self::Cancelled { provider, .. }
| Self::Streaming { provider, .. }
| Self::Other { provider, .. } => provider,
}
}
/// Check if this error is retryable
pub fn is_retryable(&self) -> bool {
match self {
Self::Network { .. }
| Self::Timeout { .. }
| Self::RateLimit { .. }
| Self::ProviderUnavailable { .. } => true,
// API errors depend on status code
Self::ApiError { status, .. } => matches!(*status, 429 | 500..=599),
// Deployment errors might be retryable depending on the issue
Self::DeploymentError { .. } => true,
// Streaming errors are typically retryable
Self::Streaming { .. } => true,
// Content filtered might be retryable with prompt changes
Self::ContentFiltered { potentially_retryable, .. } => {
potentially_retryable.unwrap_or(false)
},
// All other errors are not retryable
Self::Authentication { .. }
| Self::QuotaExceeded { .. }
| Self::ModelNotFound { .. }
| Self::InvalidRequest { .. }
| Self::NotSupported { .. }
| Self::NotImplemented { .. }
| Self::Configuration { .. }
| Self::Serialization { .. }
| Self::ContextLengthExceeded { .. }
| Self::TokenLimitExceeded { .. }
| Self::FeatureDisabled { .. }
| Self::ResponseParsing { .. }
| Self::RoutingError { .. }
| Self::TransformationError { .. }
| Self::Cancelled { .. } // User cancelled, don't retry
| Self::Other { .. } => false,
}
}
/// Get retry delay in seconds
pub fn retry_delay(&self) -> Option<u64> {
match self {
Self::RateLimit { retry_after, .. } => *retry_after,
Self::Network { .. } | Self::Timeout { .. } => Some(1),
Self::ProviderUnavailable { .. } => Some(5),
// API errors with 429 (rate limit) or 5xx get retry delays
Self::ApiError { status, .. } => match *status {
429 => Some(60), // Rate limit, wait longer
500..=599 => Some(3), // Server errors, shorter delay
_ => None,
},
// Deployment errors get a retry delay
Self::DeploymentError { .. } => Some(5),
// Streaming errors get a shorter retry delay
Self::Streaming { .. } => Some(2),
// Content filtered - conditional retry
Self::ContentFiltered {
potentially_retryable,
..
} => {
if potentially_retryable.unwrap_or(false) {
Some(10) // Allow time for prompt modification
} else {
None
}
}
// All other errors have no retry delay
Self::Authentication { .. }
| Self::QuotaExceeded { .. }
| Self::ModelNotFound { .. }
| Self::InvalidRequest { .. }
| Self::NotSupported { .. }
| Self::NotImplemented { .. }
| Self::Configuration { .. }
| Self::Serialization { .. }
| Self::ContextLengthExceeded { .. }
| Self::TokenLimitExceeded { .. }
| Self::FeatureDisabled { .. }
| Self::ResponseParsing { .. }
| Self::RoutingError { .. }
| Self::TransformationError { .. }
| Self::Cancelled { .. }
| Self::Other { .. } => None,
}
}
/// Create an error with request context for better debugging.
///
/// Returns a `ContextualError` that wraps this error with additional request information.
///
/// # Example
/// ```rust
/// # use litellm_rs::ProviderError;
/// let err = ProviderError::network("openai", "Connection refused")
/// .with_context("req-123", Some("gpt-4"));
/// ```
pub fn with_context(
self,
request_id: impl Into<String>,
model: Option<&str>,
) -> ContextualError {
ContextualError::new(self, request_id, model)
}
/// Get HTTP status code for this error
pub fn http_status(&self) -> u16 {
match self {
Self::Authentication { .. } => 401,
Self::RateLimit { .. } => 429,
Self::QuotaExceeded { .. } => 402, // Payment Required
Self::ModelNotFound { .. } => 404,
Self::InvalidRequest { .. } => 400,
Self::Configuration { .. } => 400,
Self::NotSupported { .. } => 405,
Self::NotImplemented { .. } => 501,
Self::Network { .. } | Self::Timeout { .. } | Self::ProviderUnavailable { .. } => 503,
Self::Serialization { .. } => 500,
// Enhanced error variants with appropriate HTTP status codes
Self::ContextLengthExceeded { .. } => 413, // Payload Too Large
Self::ContentFiltered { .. } => 400, // Bad Request (content policy violation)
Self::ApiError { status, .. } => *status, // Use the actual API status
Self::TokenLimitExceeded { .. } => 413, // Payload Too Large
Self::FeatureDisabled { .. } => 403, // Forbidden (feature not available)
Self::DeploymentError { .. } => 404, // Not Found (deployment not found)
Self::ResponseParsing { .. } => 502, // Bad Gateway (upstream response invalid)
Self::RoutingError { .. } => 503, // Service Unavailable (no providers available)
Self::TransformationError { .. } => 500, // Internal Server Error (conversion failed)
Self::Cancelled { .. } => 499, // Client Closed Request
Self::Streaming { .. } => 500, // Internal Server Error (streaming failed)
Self::Other { .. } => 500,
}
}
}
// Error conversions are in provider_error_conversions.rs module
/// 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) => {
::paste::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) => {
::paste::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
}
}
}
};
}
/// Default HTTP status-code → `ProviderError` mapping shared by most providers.
///
/// Providers with custom handling (e.g. Gemini, LangGraph, Databricks) should
/// implement `map_http_error` manually and call this for the status codes they
/// don't need to override.
pub fn default_http_error_mapper(
provider: &'static str,
status_code: u16,
response_body: &str,
) -> ProviderError {
match status_code {
400 => {
let message = parse_error_message_from_body(response_body)
.unwrap_or_else(|| response_body.to_string());
ProviderError::invalid_request(provider, message)
}
401 => ProviderError::authentication(provider, "Invalid API key"),
403 => ProviderError::authentication(provider, "Permission denied"),
404 => ProviderError::model_not_found(provider, "Model not found"),
429 => {
let retry_after = super::shared::parse_retry_after_from_body(response_body);
ProviderError::rate_limit(provider, retry_after)
}
500..=599 => ProviderError::api_error(provider, status_code, response_body),
_ => ProviderError::api_error(provider, status_code, response_body),
}
}
/// Try to extract an error message from a JSON response body.
///
/// Checks `error.message` and top-level `message` fields.
pub fn parse_error_message_from_body(response_body: &str) -> Option<String> {
let json: serde_json::Value = serde_json::from_str(response_body).ok()?;
json.get("error")
.and_then(|e| e.get("message"))
.and_then(|m| m.as_str())
.or_else(|| json.get("message").and_then(|m| m.as_str()))
.map(|s| s.to_string())
}
/// 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) => {
::paste::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,
)
}
}
}
};
}
/// Extended HTTP status-code → `ProviderError` mapping with additional codes.
///
/// Handles 402 (quota), 413 (context length), 408/504 (timeout), 502/503
/// (provider unavailable) in addition to the standard codes.
pub fn extended_http_error_mapper(
provider: &'static str,
status_code: u16,
response_body: &str,
) -> ProviderError {
match status_code {
400 => ProviderError::invalid_request(provider, response_body),
401 | 403 => ProviderError::authentication(provider, response_body),
402 => ProviderError::quota_exceeded(provider, response_body),
404 => ProviderError::model_not_found(provider, response_body),
408 | 504 => ProviderError::timeout(provider, response_body),
413 => ProviderError::context_length_exceeded(provider, 0, 0),
429 => ProviderError::rate_limit(provider, None),
500 => ProviderError::api_error(provider, status_code, response_body),
502 | 503 => ProviderError::provider_unavailable(provider, response_body),
_ => ProviderError::api_error(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) => {
::paste::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,
)
}
}
}
};
}