Skip to main content

erebyx_sdk/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Error types for the EREBYX SDK.
3
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("Authentication failed: {0}")]
9    AuthenticationFailed(String),
10
11    #[error("Rate limited — retry after {retry_after_secs}s")]
12    RateLimit { retry_after_secs: u64 },
13
14    #[error("Memory not found: {0}")]
15    NotFound(String),
16
17    #[error("Validation error: {0}")]
18    Validation(String),
19
20    #[error("Server error: {status} {message}")]
21    Server { status: u16, message: String },
22
23    #[error("Network error: {0}")]
24    Network(#[from] reqwest::Error),
25
26    #[error("Serialization error: {0}")]
27    Serialization(#[from] serde_json::Error),
28
29    #[error("Configuration error: {0}")]
30    Config(String),
31
32    #[error("Circuit open — memory service unavailable, will retry in {cooldown_secs}s")]
33    CircuitOpen { cooldown_secs: u64 },
34}
35
36impl Error {
37    /// Whether this error is transient and the operation should be retried.
38    pub fn is_transient(&self) -> bool {
39        matches!(
40            self,
41            Error::RateLimit { .. }
42                | Error::Network(_)
43                | Error::Server {
44                    status: 500..=599,
45                    ..
46                }
47        )
48    }
49
50    /// Whether this error should be swallowed (fail-open) by an LLM wrapper.
51    ///
52    /// Transient infrastructure failures (rate limits, network blips, 5xx
53    /// responses, open circuit breaker, malformed-server-JSON) should not
54    /// break a chat turn — the model can answer without memory and try
55    /// again next call. Caller-side errors (auth, validation, config,
56    /// missing resource) are NOT swallowable — the caller must fix them,
57    /// retrying won't help.
58    pub fn is_swallowable(&self) -> bool {
59        matches!(
60            self,
61            Error::RateLimit { .. }
62                | Error::Network(_)
63                | Error::Server {
64                    status: 500..=599,
65                    ..
66                }
67                | Error::CircuitOpen { .. }
68                | Error::Serialization(_)
69        )
70    }
71}