Skip to main content

saorsa_agent/
error.rs

1//! Error types for saorsa-agent.
2
3use saorsa_ai::SaorsaAiError;
4
5/// Error type for agent runtime operations.
6#[derive(Debug, thiserror::Error)]
7pub enum SaorsaAgentError {
8    /// Tool execution failed.
9    #[error("tool error: {0}")]
10    Tool(String),
11
12    /// Session management error.
13    #[error("session error: {0}")]
14    Session(String),
15
16    /// Context engineering error.
17    #[error("context error: {0}")]
18    Context(String),
19
20    /// LLM provider error.
21    #[error("provider error: {0}")]
22    Provider(#[from] SaorsaAiError),
23
24    /// Agent run was cancelled.
25    #[error("cancelled: {0}")]
26    Cancelled(String),
27
28    /// I/O error.
29    #[error("I/O error: {0}")]
30    Io(#[from] std::io::Error),
31
32    /// JSON error.
33    #[error("JSON error: {0}")]
34    Json(#[from] serde_json::Error),
35
36    /// Internal error.
37    #[error("internal error: {0}")]
38    Internal(String),
39
40    /// Extension error.
41    #[error("extension error: {0}")]
42    Extension(String),
43
44    /// Home directory could not be determined.
45    #[error("could not determine home directory")]
46    HomeDirectory,
47
48    /// Configuration file I/O error.
49    #[error("config I/O error: {0}")]
50    ConfigIo(std::io::Error),
51
52    /// Configuration file parse error.
53    #[error("config parse error: {0}")]
54    ConfigParse(serde_json::Error),
55
56    /// Environment variable not found.
57    #[error("environment variable not found: {name}")]
58    EnvVarNotFound {
59        /// The name of the missing environment variable.
60        name: String,
61    },
62
63    /// Shell command execution failed.
64    #[error("command execution failed: {0}")]
65    CommandFailed(String),
66}
67
68/// Result type alias for saorsa-agent operations.
69pub type Result<T> = std::result::Result<T, SaorsaAgentError>;
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn tool_error_display() {
77        let err = SaorsaAgentError::Tool("command failed".into());
78        assert_eq!(err.to_string(), "tool error: command failed");
79    }
80
81    #[test]
82    fn provider_error_converts() {
83        let ai_err = SaorsaAiError::Auth("bad key".into());
84        let err: SaorsaAgentError = ai_err.into();
85        assert!(matches!(err, SaorsaAgentError::Provider(_)));
86    }
87}