use thiserror::Error as ThisError;
#[derive(Debug, Clone, ThisError)]
#[non_exhaustive]
pub enum Error {
#[error("{message}")]
Api {
message: String,
http_status: Option<u16>,
retry_after: Option<u64>,
},
#[error("Network error: {message}")]
Network {
message: String,
},
#[error("Invalid argument: {message}")]
InvalidArgument {
message: String,
},
}
impl Error {
pub fn error_type(&self) -> &'static str {
match self {
Error::Api { .. } => "api",
Error::Network { .. } => "network",
Error::InvalidArgument { .. } => "invalid_argument",
}
}
pub fn message(&self) -> &str {
match self {
Error::Api { message, .. } => message,
Error::Network { message } => message,
Error::InvalidArgument { message } => message,
}
}
pub fn http_status(&self) -> Option<u16> {
match self {
Error::Api { http_status, .. } => *http_status,
_ => None,
}
}
pub fn retry_after(&self) -> Option<u64> {
match self {
Error::Api { retry_after, .. } => *retry_after,
_ => None,
}
}
pub(crate) fn invalid_argument(msg: impl Into<String>) -> Self {
Error::InvalidArgument {
message: msg.into(),
}
}
pub(crate) fn network(msg: impl Into<String>) -> Self {
Error::Network {
message: msg.into(),
}
}
pub(crate) fn api(
message: impl Into<String>,
http_status: Option<u16>,
retry_after: Option<u64>,
) -> Self {
Error::Api {
message: message.into(),
http_status,
retry_after,
}
}
}
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
Error::Network {
message: e.to_string(),
}
}
}