use std::fmt;
#[derive(Debug, Clone)]
pub struct BilldogEngError {
pub message: String,
pub status: u16,
pub code: Option<String>,
pub body: Option<serde_json::Value>,
}
impl BilldogEngError {
pub fn new(message: impl Into<String>, status: u16) -> Self {
Self {
message: message.into(),
status,
code: None,
body: None,
}
}
pub fn with_code(mut self, code: Option<String>) -> Self {
self.code = code;
self
}
pub fn with_body(mut self, body: Option<serde_json::Value>) -> Self {
self.body = body;
self
}
pub fn is_retryable(&self) -> bool {
is_retryable_status(self.status)
}
}
impl fmt::Display for BilldogEngError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BilldogEngError(status={}): {}", self.status, self.message)
}
}
impl std::error::Error for BilldogEngError {}
pub fn is_retryable_status(status: u16) -> bool {
status == 0 || status == 408 || status == 429 || (500..=599).contains(&status)
}
pub type Result<T> = std::result::Result<T, BilldogEngError>;