use std::fmt;
#[derive(Debug)]
pub enum ApiError {
RequestError(reqwest::Error),
HttpError(u16),
UrlError(url::ParseError),
JsonError(serde_json::Error),
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ApiError::RequestError(e) => write!(f, "请求错误: {}", e),
ApiError::HttpError(status) => write!(f, "HTTP 错误: {}", status),
ApiError::UrlError(e) => write!(f, "URL 解析错误: {}", e),
ApiError::JsonError(e) => write!(f, "JSON 错误: {}", e),
}
}
}
impl std::error::Error for ApiError {}
impl From<reqwest::Error> for ApiError {
fn from(error: reqwest::Error) -> Self {
ApiError::RequestError(error)
}
}
impl From<url::ParseError> for ApiError {
fn from(error: url::ParseError) -> Self {
ApiError::UrlError(error)
}
}
impl From<serde_json::Error> for ApiError {
fn from(error: serde_json::Error) -> Self {
ApiError::JsonError(error)
}
}
pub type Result<T> = std::result::Result<T, ApiError>;