use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ApiError {
pub code: u32,
pub message: String,
pub errors: Option<serde_json::Value>,
pub trace_id: Option<String>,
}
impl ApiError {
pub fn new(code: u32, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
errors: None,
trace_id: None,
}
}
pub fn is_rate_limit(&self) -> bool {
self.code == 429
}
pub fn is_auth_error(&self) -> bool {
self.code == 401 || self.code == 403
}
pub fn is_not_found(&self) -> bool {
self.code == 404
}
pub fn is_server_error(&self) -> bool {
self.code >= 500
}
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "API Error {}: {}", self.code, self.message)
}
}
impl std::error::Error for ApiError {}