use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuraError {
pub status: u16,
pub code: String,
pub message: String,
pub details: Option<serde_json::Value>,
}
impl std::error::Error for AuraError {}
impl fmt::Display for AuraError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AuraError: [{}] {} (status: {})",
self.code, self.message, self.status
)
}
}
impl AuraError {
pub fn new(status: u16, code: &str, message: &str, details: Option<serde_json::Value>) -> Self {
Self {
status,
code: code.to_string(),
message: message.to_string(),
details,
}
}
pub fn network(err_msg: &str) -> Self {
Self::new(0, "network_error", err_msg, None)
}
pub fn serialization(err_msg: &str) -> Self {
Self::new(0, "serialization_error", err_msg, None)
}
pub fn is_not_found(&self) -> bool {
self.status == 404
}
pub fn is_unauthorized(&self) -> bool {
self.status == 401
}
pub fn is_forbidden(&self) -> bool {
self.status == 403
}
pub fn is_rate_limited(&self) -> bool {
self.status == 429
}
}