use std::fmt;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{status} {name}: {message}")]
Api {
status: u16,
name: String,
message: String,
},
#[error("{0}")]
Signature(String),
#[error("{0}")]
Config(String),
#[error("JSON: {0}")]
Json(#[from] serde_json::Error),
}
impl Error {
pub fn status(&self) -> Option<u16> {
match self {
Error::Api { status, .. } => Some(*status),
_ => None,
}
}
pub fn name(&self) -> Option<&str> {
match self {
Error::Api { name, .. } => Some(name),
_ => None,
}
}
pub fn retryable(&self) -> bool {
matches!(self, Error::Api { status, .. } if *status == 0 || *status == 429 || *status >= 500)
}
pub(crate) fn unreachable(err: impl fmt::Display) -> Self {
Error::Api {
status: 0,
name: "application_error".into(),
message: format!("Unable to reach the API: {err}"),
}
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;