use std::time::Duration;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("oblodai: API error {code} (HTTP {status}): {message}")]
Api {
code: String,
message: String,
status: u16,
raw: String,
retry_after: Option<Duration>,
},
#[error("oblodai: connection error: {0}")]
Connection(String),
#[error("oblodai: signature error: {0}")]
Signature(String),
#[error("oblodai: serialization error: {0}")]
Serialization(String),
#[error("oblodai: config error: {0}")]
Config(String),
}
impl Error {
pub fn is_retriable(&self) -> bool {
match self {
Error::Api { status, .. } => *status >= 500 || *status == 429,
Error::Connection(_) => true,
_ => false,
}
}
pub fn code(&self) -> Option<&str> {
match self {
Error::Api { code, .. } => Some(code),
_ => None,
}
}
pub fn retry_after(&self) -> Option<Duration> {
match self {
Error::Api { retry_after, .. } => *retry_after,
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;