Skip to main content

adk_core/
error.rs

1/// Unified error type for all ADK operations.
2///
3/// Each variant corresponds to a layer in the framework. Prefer the most
4/// specific variant (e.g. [`Tool`](Self::Tool) over [`Agent`](Self::Agent))
5/// so callers can match on the source of failure.
6#[derive(Debug, thiserror::Error)]
7pub enum AdkError {
8    /// Error originating from agent execution or orchestration.
9    #[error("Agent error: {0}")]
10    Agent(String),
11
12    /// Error from an LLM provider (network, auth, rate limit, bad response).
13    #[error("Model error: {0}")]
14    Model(String),
15
16    /// Error from tool execution or schema validation.
17    #[error("Tool error: {0}")]
18    Tool(String),
19
20    /// Error from session creation, retrieval, or state persistence.
21    #[error("Session error: {0}")]
22    Session(String),
23
24    /// Error from artifact storage or retrieval.
25    #[error("Artifact error: {0}")]
26    Artifact(String),
27
28    /// Error from the long-term memory / RAG subsystem.
29    #[error("Memory error: {0}")]
30    Memory(String),
31
32    /// Invalid or missing configuration (API keys, model names, etc.).
33    #[error("Configuration error: {0}")]
34    Config(String),
35
36    /// Filesystem or network I/O error.
37    #[error("IO error: {0}")]
38    Io(#[from] std::io::Error),
39
40    /// JSON serialization or deserialization error.
41    #[error("Serialization error: {0}")]
42    Serde(#[from] serde_json::Error),
43}
44
45/// Convenience alias used throughout ADK crates.
46pub type Result<T> = std::result::Result<T, AdkError>;
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_error_display() {
54        let err = AdkError::Agent("test error".to_string());
55        assert_eq!(err.to_string(), "Agent error: test error");
56    }
57
58    #[test]
59    fn test_error_from_io() {
60        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
61        let adk_err: AdkError = io_err.into();
62        assert!(matches!(adk_err, AdkError::Io(_)));
63    }
64
65    #[test]
66    #[allow(clippy::unnecessary_literal_unwrap)]
67    fn test_result_type() {
68        let ok_result: Result<i32> = Ok(42);
69        assert_eq!(ok_result.unwrap(), 42);
70
71        let err_result: Result<i32> = Err(AdkError::Config("invalid".to_string()));
72        assert!(err_result.is_err());
73    }
74}