1use std::fmt;
12
13pub type Result<T> = std::result::Result<T, Error>;
15
16#[derive(Debug, Clone)]
22pub struct HttpError {
23 pub status_code: u16,
25 pub r#type: String,
27 pub retry_after: i64,
29 pub body: String,
31 pub message: String,
33}
34
35impl HttpError {
36 pub fn new(
38 status_code: u16,
39 r#type: impl Into<String>,
40 message: impl Into<String>,
41 retry_after: i64,
42 body: impl Into<String>,
43 ) -> Self {
44 Self {
45 status_code,
46 r#type: r#type.into(),
47 retry_after,
48 body: body.into(),
49 message: message.into(),
50 }
51 }
52}
53
54impl fmt::Display for HttpError {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 if !self.r#type.is_empty() {
58 write!(
59 f,
60 "HTTP {}: [{}] {}",
61 self.status_code, self.r#type, self.message
62 )
63 } else if !self.message.is_empty() {
64 write!(f, "HTTP {}: {}", self.status_code, self.message)
65 } else if !self.body.is_empty() {
66 write!(f, "HTTP {}: {}", self.status_code, self.body)
67 } else {
68 write!(f, "HTTP {}", self.status_code)
69 }
70 }
71}
72
73impl std::error::Error for HttpError {}
74
75#[derive(Debug, Clone)]
77pub struct NetworkError {
78 pub op: String,
80 pub url: String,
82 pub cause: String,
84 pub timeout: bool,
85 pub eof: bool,
86}
87
88impl NetworkError {
89 pub fn new(op: impl Into<String>, url: impl Into<String>, cause: impl Into<String>) -> Self {
90 let cause = cause.into();
91 Self {
92 op: op.into(),
93 url: url.into(),
94 cause: if cause.is_empty() {
95 "network error".to_string()
96 } else {
97 cause
98 },
99 timeout: false,
100 eof: false,
101 }
102 }
103 pub fn is_timeout(&self) -> bool {
105 self.timeout
106 }
107 pub fn is_eof(&self) -> bool {
108 self.eof
109 }
110}
111
112impl fmt::Display for NetworkError {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 write!(f, "{} {}: {}", self.op, self.url, self.cause)
115 }
116}
117
118impl std::error::Error for NetworkError {}
119
120#[derive(Debug, Clone, Default)]
122pub struct StreamError {
123 pub code: String,
125 pub stage: String,
127 pub user_message: String,
129 pub raw_error: String,
131 pub retryable: bool,
133}
134
135impl fmt::Display for StreamError {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 let body = if !self.raw_error.is_empty() {
138 &self.raw_error
139 } else {
140 &self.user_message
141 };
142 if !self.stage.is_empty() {
143 write!(f, "stream failed: {}: {}", self.stage, body)
144 } else {
145 write!(f, "stream failed: {body}")
146 }
147 }
148}
149
150impl std::error::Error for StreamError {}
151
152#[derive(Debug, thiserror::Error)]
158pub enum Error {
159 #[error("{message}")]
161 RateLimit {
162 message: String,
163 retry_after: String,
164 raw: String,
165 },
166
167 #[error("API error (code={code}): {message}")]
169 Business { code: i64, message: String },
170
171 #[error("order {order_id} terminated: {status}")]
173 OrderTerminal { order_id: String, status: String },
174
175 #[error(
177 "managed model \"{model_id}\" not found (list models to refresh cache, or verify model id)"
178 )]
179 ModelNotFound { model_id: String },
180
181 #[error(transparent)]
183 Http(#[from] HttpError),
184
185 #[error(transparent)]
187 Network(#[from] NetworkError),
188
189 #[error(transparent)]
191 Stream(#[from] StreamError),
192
193 #[error("agent run failed: {}{message}", stage.as_ref().map(|s| format!("{s}: ")).unwrap_or_default())]
196 AgentRunStream {
197 code: String,
198 stage: Option<String>,
199 message: String,
200 retryable: bool,
201 },
202
203 #[error(transparent)]
206 OAuthTokenEndpoint(#[from] crate::auth::auth::OAuthTokenEndpointError),
207
208 #[error(transparent)]
210 Json(#[from] serde_json::Error),
211 #[cfg(feature = "native-http")]
212 #[error(transparent)]
213 Http2(#[from] reqwest::Error),
214 #[error(transparent)]
215 Io(#[from] std::io::Error),
216 #[error("{0}")]
218 Other(String),
219}
220
221impl Error {
222 pub fn rate_limit(
224 message: impl Into<String>,
225 retry_after: impl Into<String>,
226 raw: impl Into<String>,
227 ) -> Self {
228 Error::RateLimit {
229 message: message.into(),
230 retry_after: retry_after.into(),
231 raw: raw.into(),
232 }
233 }
234 pub fn business(code: i64, message: impl Into<String>) -> Self {
236 Error::Business {
237 code,
238 message: message.into(),
239 }
240 }
241 pub fn other(message: impl Into<String>) -> Self {
243 Error::Other(message.into())
244 }
245}