use thiserror::Error;
#[derive(Error, Debug)]
pub enum ApiError {
#[error("HTTP error {status}: {text}")]
Http {
status: reqwest::StatusCode,
text: String,
},
#[error("Reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("EventSource error: {0}")]
EventSource(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Other(String),
#[error("Unknown error")]
Unknown,
}
pub type Result<T> = std::result::Result<T, ApiError>;
impl ApiError {
pub fn http_error(status: reqwest::StatusCode, text: impl Into<String>) -> Self {
ApiError::Http {
status,
text: text.into(),
}
}
}
impl From<&str> for ApiError {
fn from(s: &str) -> Self {
ApiError::Other(s.to_string())
}
}
impl From<String> for ApiError {
fn from(s: String) -> Self {
ApiError::Other(s)
}
}