1use graphrt_core::NodeError;
4
5#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
7#[non_exhaustive]
8pub enum RagError {
9 #[error("configuration error: {0}")]
11 Config(String),
12 #[error("invalid input: {0}")]
14 Input(String),
15 #[error("authentication failed: {0}")]
17 Auth(String),
18 #[error("rate limited: {message}")]
20 RateLimit { message: String, retry_after_ms: Option<u64> },
21 #[error("HTTP {status}: {message}")]
23 Http { status: u16, message: String },
24 #[error("network error: {0}")]
26 Network(String),
27 #[error("invalid response: {0}")]
29 Decode(String),
30 #[error("store error: {0}")]
32 Store(String),
33 #[error("not found: {0}")]
35 NotFound(String),
36 #[error("I/O error: {0}")]
38 Io(String),
39}
40
41impl RagError {
42 pub fn is_retryable(&self) -> bool {
44 match self {
45 RagError::RateLimit { .. } | RagError::Network(_) => true,
46 RagError::Http { status, .. } => *status >= 500 || *status == 408,
47 _ => false,
48 }
49 }
50
51 pub fn kind(&self) -> &'static str {
53 match self {
54 RagError::Config(_) => "config",
55 RagError::Input(_) => "invalid_input",
56 RagError::Auth(_) => "auth",
57 RagError::RateLimit { .. } => "rate_limit",
58 RagError::Http { .. } => "http_status",
59 RagError::Network(_) => "network",
60 RagError::Decode(_) => "invalid_response",
61 RagError::Store(_) => "store",
62 RagError::NotFound(_) => "not_found",
63 RagError::Io(_) => "io",
64 }
65 }
66}
67
68impl From<RagError> for NodeError {
69 fn from(e: RagError) -> Self {
70 let retry_after = match &e {
71 RagError::RateLimit { retry_after_ms, .. } => *retry_after_ms,
72 _ => None,
73 };
74 let base = if e.is_retryable() {
75 NodeError::new(e.kind(), e.to_string())
76 } else {
77 NodeError::fatal(e.kind(), e.to_string())
78 };
79 base.with_retry_after(retry_after)
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn retryable_mapping() {
89 assert!(RagError::Network("x".into()).is_retryable());
90 assert!(RagError::Http { status: 503, message: String::new() }.is_retryable());
91 assert!(!RagError::Http { status: 400, message: String::new() }.is_retryable());
92 let ne: NodeError = RagError::RateLimit { message: "slow".into(), retry_after_ms: Some(1500) }.into();
93 assert!(ne.retryable);
94 assert_eq!(ne.retry_after_ms, Some(1500));
95 let ne: NodeError = RagError::Auth("bad key".into()).into();
96 assert!(!ne.retryable);
97 }
98}