1use serde::{Deserialize, Serialize};
7use thiserror::Error;
8
9pub type ProviderResult<T> = Result<T, ProviderError>;
11
12#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub enum ProviderError {
15 #[error("Authentication failed: {message}")]
17 Authentication { message: String },
18
19 #[error("Transport error: {message}")]
21 Transport { message: String },
22
23 #[error("Rate limit exceeded: {message}")]
25 RateLimit {
26 message: String,
27 retry_after: Option<u64>,
28 },
29
30 #[error("Malformed response: {message}")]
32 MalformedResponse { message: String },
33
34 #[error("Invalid request: {message}")]
36 InvalidRequest { message: String },
37
38 #[error("Model error: {message}")]
40 Model { message: String },
41
42 #[error("Provider error: {message}")]
44 General { message: String },
45}
46
47impl ProviderError {
48 pub fn auth<S: Into<String>>(message: S) -> Self {
50 Self::Authentication {
51 message: message.into(),
52 }
53 }
54
55 pub fn transport<S: Into<String>>(message: S) -> Self {
57 Self::Transport {
58 message: message.into(),
59 }
60 }
61
62 pub fn rate_limit<S: Into<String>>(message: S, retry_after: Option<u64>) -> Self {
64 Self::RateLimit {
65 message: message.into(),
66 retry_after,
67 }
68 }
69
70 pub fn malformed<S: Into<String>>(message: S) -> Self {
72 Self::MalformedResponse {
73 message: message.into(),
74 }
75 }
76
77 pub fn invalid_request<S: Into<String>>(message: S) -> Self {
79 Self::InvalidRequest {
80 message: message.into(),
81 }
82 }
83
84 pub fn model<S: Into<String>>(message: S) -> Self {
86 Self::Model {
87 message: message.into(),
88 }
89 }
90
91 pub fn general<S: Into<String>>(message: S) -> Self {
93 Self::General {
94 message: message.into(),
95 }
96 }
97
98 pub fn is_authentication(&self) -> bool {
100 matches!(self, Self::Authentication { .. })
101 }
102
103 pub fn is_rate_limit(&self) -> bool {
105 matches!(self, Self::RateLimit { .. })
106 }
107
108 pub fn is_transport(&self) -> bool {
110 matches!(self, Self::Transport { .. })
111 }
112
113 pub fn retry_after(&self) -> Option<u64> {
115 match self {
116 Self::RateLimit { retry_after, .. } => *retry_after,
117 _ => None,
118 }
119 }
120}