use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct HttpError {
pub status_code: u16,
pub r#type: String,
pub retry_after: i64,
pub body: String,
pub message: String,
}
impl HttpError {
pub fn new(
status_code: u16,
r#type: impl Into<String>,
message: impl Into<String>,
retry_after: i64,
body: impl Into<String>,
) -> Self {
Self {
status_code,
r#type: r#type.into(),
retry_after,
body: body.into(),
message: message.into(),
}
}
}
impl fmt::Display for HttpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.r#type.is_empty() {
write!(
f,
"HTTP {}: [{}] {}",
self.status_code, self.r#type, self.message
)
} else if !self.message.is_empty() {
write!(f, "HTTP {}: {}", self.status_code, self.message)
} else if !self.body.is_empty() {
write!(f, "HTTP {}: {}", self.status_code, self.body)
} else {
write!(f, "HTTP {}", self.status_code)
}
}
}
impl std::error::Error for HttpError {}
#[derive(Debug, Clone)]
pub struct NetworkError {
pub op: String,
pub url: String,
pub cause: String,
pub timeout: bool,
pub eof: bool,
}
impl NetworkError {
pub fn new(op: impl Into<String>, url: impl Into<String>, cause: impl Into<String>) -> Self {
let cause = cause.into();
Self {
op: op.into(),
url: url.into(),
cause: if cause.is_empty() {
"network error".to_string()
} else {
cause
},
timeout: false,
eof: false,
}
}
pub fn is_timeout(&self) -> bool {
self.timeout
}
pub fn is_eof(&self) -> bool {
self.eof
}
}
impl fmt::Display for NetworkError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}: {}", self.op, self.url, self.cause)
}
}
impl std::error::Error for NetworkError {}
#[derive(Debug, Clone, Default)]
pub struct StreamError {
pub code: String,
pub stage: String,
pub user_message: String,
pub raw_error: String,
pub retryable: bool,
}
impl fmt::Display for StreamError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let body = if !self.raw_error.is_empty() {
&self.raw_error
} else {
&self.user_message
};
if !self.stage.is_empty() {
write!(f, "stream failed: {}: {}", self.stage, body)
} else {
write!(f, "stream failed: {body}")
}
}
}
impl std::error::Error for StreamError {}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{message}")]
RateLimit {
message: String,
retry_after: String,
raw: String,
},
#[error("API error (code={code}): {message}")]
Business { code: i64, message: String },
#[error("order {order_id} terminated: {status}")]
OrderTerminal { order_id: String, status: String },
#[error(
"managed model \"{model_id}\" not found (list models to refresh cache, or verify model id)"
)]
ModelNotFound { model_id: String },
#[error(transparent)]
Http(#[from] HttpError),
#[error(transparent)]
Network(#[from] NetworkError),
#[error(transparent)]
Stream(#[from] StreamError),
#[error("agent run failed: {}{message}", stage.as_ref().map(|s| format!("{s}: ")).unwrap_or_default())]
AgentRunStream {
code: String,
stage: Option<String>,
message: String,
retryable: bool,
},
#[error(transparent)]
OAuthTokenEndpoint(#[from] crate::auth::auth::OAuthTokenEndpointError),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[cfg(feature = "native-http")]
#[error(transparent)]
Http2(#[from] reqwest::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("{0}")]
Other(String),
}
impl Error {
pub fn rate_limit(
message: impl Into<String>,
retry_after: impl Into<String>,
raw: impl Into<String>,
) -> Self {
Error::RateLimit {
message: message.into(),
retry_after: retry_after.into(),
raw: raw.into(),
}
}
pub fn business(code: i64, message: impl Into<String>) -> Self {
Error::Business {
code,
message: message.into(),
}
}
pub fn other(message: impl Into<String>) -> Self {
Error::Other(message.into())
}
}