use std::fmt;
#[derive(Debug, thiserror::Error)]
pub enum GuardAgentError {
#[error("Invalid agent configuration: {0}")]
Config(#[from] ConfigError),
#[error("Buffer full at capacity {capacity} and buffer_overflow_policy='raise'")]
BufferFull {
capacity: usize,
},
#[error("Rate limited (429); retry after {retry_after_seconds:.0}s")]
RateLimited {
retry_after_seconds: f64,
},
#[error("Permanent client error {status_code}: {detail}")]
Permanent {
status_code: u16,
detail: String,
},
#[error("Payload too large (413): {detail}")]
PayloadTooLarge {
detail: String,
},
#[error("{0}")]
Transport(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Redis error: {0}")]
Redis(String),
#[error("{0}")]
Encryption(String),
#[error("{0}")]
EncryptionConfig(String),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("Invalid agent configuration: {}", problems.join("; "))]
pub struct ConfigError {
pub problems: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorStage {
TransportSend,
FlushEvents,
FlushMetrics,
Encryption,
}
impl fmt::Display for ErrorStage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = match self {
Self::TransportSend => "transport_send",
Self::FlushEvents => "flush_events",
Self::FlushMetrics => "flush_metrics",
Self::Encryption => "encryption",
};
f.write_str(label)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn config_error_joins_problems() {
let err = ConfigError {
problems: vec![
"api_key must be at least 10 characters long".into(),
"buffer_size must be greater than 0".into(),
],
};
assert_eq!(
err.to_string(),
"Invalid agent configuration: api_key must be at least 10 characters long; \
buffer_size must be greater than 0"
);
}
#[test]
fn error_stage_labels_match_python_stages() {
assert_eq!(ErrorStage::TransportSend.to_string(), "transport_send");
assert_eq!(ErrorStage::FlushEvents.to_string(), "flush_events");
assert_eq!(ErrorStage::FlushMetrics.to_string(), "flush_metrics");
assert_eq!(ErrorStage::Encryption.to_string(), "encryption");
}
#[test]
fn error_messages_are_stable() {
let rate = GuardAgentError::RateLimited {
retry_after_seconds: 42.0,
};
assert_eq!(rate.to_string(), "Rate limited (429); retry after 42s");
let perm = GuardAgentError::Permanent {
status_code: 400,
detail: "bad request".into(),
};
assert_eq!(perm.to_string(), "Permanent client error 400: bad request");
let full = GuardAgentError::BufferFull { capacity: 10 };
assert_eq!(
full.to_string(),
"Buffer full at capacity 10 and buffer_overflow_policy='raise'"
);
}
}