use std::error::Error as StdError;
use std::fmt;
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "reqwest")]
Reqwest(reqwest::Error),
#[cfg(feature = "dotenvy")]
Env(dotenvy::Error),
MissingApiKey,
ApiError {
status: u16,
message: String,
request_id: Option<String>,
},
Custom(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "reqwest")]
Error::Reqwest(e) => write!(f, "HTTP request error: {}", e),
#[cfg(feature = "dotenvy")]
Error::Env(e) => write!(f, "Environment variable error: {}", e),
Error::MissingApiKey => write!(f, "Missing API key"),
Error::ApiError {
status,
message,
request_id,
} => {
write!(f, "API error ({}): {}", status, message)?;
if let Some(id) = request_id {
write!(f, " [request_id: {}]", id)?;
}
Ok(())
}
Error::Custom(s) => write!(f, "{}", s),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
#[cfg(feature = "reqwest")]
Error::Reqwest(e) => Some(e),
#[cfg(feature = "dotenvy")]
Error::Env(e) => Some(e),
_ => None,
}
}
}
#[cfg(feature = "reqwest")]
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}
#[cfg(feature = "dotenvy")]
impl From<dotenvy::Error> for Error {
fn from(e: dotenvy::Error) -> Self {
Error::Env(e)
}
}
#[cfg(feature = "decoder")]
impl From<decoder::Error> for Error {
fn from(e: decoder::Error) -> Self {
Error::Custom(e.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Custom(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;