use std::fmt;
#[derive(Debug)]
pub enum Error {
RequestError(reqwest::Error),
JsonError(serde_json::Error),
ApiError {
status: u16,
message: String,
},
AuthError(String),
InvalidInput(String),
NotFound(String),
Other(String),
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::RequestError(e) => write!(f, "Request error: {}", e),
Error::JsonError(e) => write!(f, "JSON error: {}", e),
Error::ApiError { status, message } => {
write!(f, "API error ({}): {}", status, message)
}
Error::AuthError(msg) => write!(f, "Authentication error: {}", msg),
Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
Error::NotFound(msg) => write!(f, "Not found: {}", msg),
Error::Other(msg) => write!(f, "Error: {}", msg),
}
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Error::RequestError(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::JsonError(err)
}
}
pub type Result<T> = std::result::Result<T, Error>;