Skip to main content

microclaw_core/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4#[allow(dead_code)]
5pub enum MicroClawError {
6    #[error("LLM API error: {0}")]
7    LlmApi(String),
8
9    #[error("Rate limited, retry after backoff")]
10    RateLimited,
11
12    #[cfg(feature = "sqlite-errors")]
13    #[error("Database error: {0}")]
14    Database(#[from] rusqlite::Error),
15
16    #[cfg(feature = "http-errors")]
17    #[error("HTTP error: {0}")]
18    Http(#[from] reqwest::Error),
19
20    #[error("JSON error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    #[error("IO error: {0}")]
24    Io(#[from] std::io::Error),
25
26    #[error("Tool execution error: {0}")]
27    ToolExecution(String),
28
29    #[error("Config error: {0}")]
30    Config(String),
31
32    #[error("Max tool iterations reached ({0})")]
33    MaxIterations(usize),
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_error_display_messages() {
42        let e = MicroClawError::LlmApi("bad request".into());
43        assert_eq!(e.to_string(), "LLM API error: bad request");
44
45        let e = MicroClawError::RateLimited;
46        assert_eq!(e.to_string(), "Rate limited, retry after backoff");
47
48        let e = MicroClawError::ToolExecution("tool failed".into());
49        assert_eq!(e.to_string(), "Tool execution error: tool failed");
50
51        let e = MicroClawError::Config("missing key".into());
52        assert_eq!(e.to_string(), "Config error: missing key");
53
54        let e = MicroClawError::MaxIterations(25);
55        assert_eq!(e.to_string(), "Max tool iterations reached (25)");
56    }
57
58    #[test]
59    fn test_error_from_io() {
60        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
61        let e: MicroClawError = io_err.into();
62        assert!(e.to_string().contains("not found"));
63    }
64
65    #[test]
66    fn test_error_from_json() {
67        let json_err = serde_json::from_str::<serde_json::Value>("{{invalid").unwrap_err();
68        let e: MicroClawError = json_err.into();
69        assert!(e.to_string().contains("JSON error"));
70    }
71
72    #[test]
73    fn test_error_debug() {
74        let e = MicroClawError::RateLimited;
75        let debug = format!("{:?}", e);
76        assert!(debug.contains("RateLimited"));
77    }
78}