ricecoder_agents/domain/
error.rs

1//! Error types for domain-specific agents
2
3use thiserror::Error;
4
5/// Errors that can occur during domain agent operations
6#[derive(Debug, Clone, Error)]
7pub enum DomainError {
8    /// Domain agent not found
9    #[error("Domain agent not found: {0}")]
10    AgentNotFound(String),
11
12    /// Domain not found
13    #[error("Domain not found: {0}")]
14    DomainNotFound(String),
15
16    /// Knowledge not found
17    #[error("Knowledge not found: {0}")]
18    KnowledgeNotFound(String),
19
20    /// Context error
21    #[error("Context error: {0}")]
22    ContextError(String),
23
24    /// Conflict error
25    #[error("Conflict error: {0}")]
26    ConflictError(String),
27
28    /// Configuration error
29    #[error("Configuration error: {0}")]
30    ConfigError(String),
31
32    /// Coordination error
33    #[error("Coordination error: {0}")]
34    CoordinationError(String),
35
36    /// Invalid input
37    #[error("Invalid input: {0}")]
38    InvalidInput(String),
39
40    /// Serialization error
41    #[error("Serialization error: {0}")]
42    SerializationError(String),
43
44    /// Internal error
45    #[error("Internal error: {0}")]
46    Internal(String),
47}
48
49impl DomainError {
50    /// Create a new AgentNotFound error
51    pub fn agent_not_found(domain: impl Into<String>) -> Self {
52        Self::AgentNotFound(domain.into())
53    }
54
55    /// Create a new DomainNotFound error
56    pub fn domain_not_found(domain: impl Into<String>) -> Self {
57        Self::DomainNotFound(domain.into())
58    }
59
60    /// Create a new KnowledgeNotFound error
61    pub fn knowledge_not_found(knowledge: impl Into<String>) -> Self {
62        Self::KnowledgeNotFound(knowledge.into())
63    }
64
65    /// Create a new ContextError
66    pub fn context_error(reason: impl Into<String>) -> Self {
67        Self::ContextError(reason.into())
68    }
69
70    /// Create a new ConflictError
71    pub fn conflict_error(reason: impl Into<String>) -> Self {
72        Self::ConflictError(reason.into())
73    }
74
75    /// Create a new ConfigError
76    pub fn config_error(reason: impl Into<String>) -> Self {
77        Self::ConfigError(reason.into())
78    }
79
80    /// Create a new CoordinationError
81    pub fn coordination_error(reason: impl Into<String>) -> Self {
82        Self::CoordinationError(reason.into())
83    }
84
85    /// Create a new InvalidInput error
86    pub fn invalid_input(reason: impl Into<String>) -> Self {
87        Self::InvalidInput(reason.into())
88    }
89
90    /// Create a new SerializationError
91    pub fn serialization_error(reason: impl Into<String>) -> Self {
92        Self::SerializationError(reason.into())
93    }
94
95    /// Create a new Internal error
96    pub fn internal(reason: impl Into<String>) -> Self {
97        Self::Internal(reason.into())
98    }
99}
100
101/// Result type for domain agent operations
102pub type DomainResult<T> = std::result::Result<T, DomainError>;
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_domain_error_agent_not_found() {
110        let error = DomainError::agent_not_found("web");
111        assert!(matches!(error, DomainError::AgentNotFound(_)));
112        assert_eq!(error.to_string(), "Domain agent not found: web");
113    }
114
115    #[test]
116    fn test_domain_error_domain_not_found() {
117        let error = DomainError::domain_not_found("mobile");
118        assert!(matches!(error, DomainError::DomainNotFound(_)));
119        assert_eq!(error.to_string(), "Domain not found: mobile");
120    }
121
122    #[test]
123    fn test_domain_error_knowledge_not_found() {
124        let error = DomainError::knowledge_not_found("react");
125        assert!(matches!(error, DomainError::KnowledgeNotFound(_)));
126        assert_eq!(error.to_string(), "Knowledge not found: react");
127    }
128
129    #[test]
130    fn test_domain_error_context_error() {
131        let error = DomainError::context_error("invalid context");
132        assert!(matches!(error, DomainError::ContextError(_)));
133        assert_eq!(error.to_string(), "Context error: invalid context");
134    }
135
136    #[test]
137    fn test_domain_error_conflict_error() {
138        let error = DomainError::conflict_error("conflicting recommendations");
139        assert!(matches!(error, DomainError::ConflictError(_)));
140        assert_eq!(error.to_string(), "Conflict error: conflicting recommendations");
141    }
142
143    #[test]
144    fn test_domain_error_config_error() {
145        let error = DomainError::config_error("invalid config");
146        assert!(matches!(error, DomainError::ConfigError(_)));
147        assert_eq!(error.to_string(), "Configuration error: invalid config");
148    }
149
150    #[test]
151    fn test_domain_error_coordination_error() {
152        let error = DomainError::coordination_error("coordination failed");
153        assert!(matches!(error, DomainError::CoordinationError(_)));
154        assert_eq!(error.to_string(), "Coordination error: coordination failed");
155    }
156
157    #[test]
158    fn test_domain_error_invalid_input() {
159        let error = DomainError::invalid_input("invalid data");
160        assert!(matches!(error, DomainError::InvalidInput(_)));
161        assert_eq!(error.to_string(), "Invalid input: invalid data");
162    }
163
164    #[test]
165    fn test_domain_error_serialization_error() {
166        let error = DomainError::serialization_error("invalid json");
167        assert!(matches!(error, DomainError::SerializationError(_)));
168        assert_eq!(error.to_string(), "Serialization error: invalid json");
169    }
170
171    #[test]
172    fn test_domain_error_internal() {
173        let error = DomainError::internal("internal error");
174        assert!(matches!(error, DomainError::Internal(_)));
175        assert_eq!(error.to_string(), "Internal error: internal error");
176    }
177
178    #[test]
179    fn test_domain_error_clone() {
180        let error = DomainError::agent_not_found("web");
181        let cloned = error.clone();
182        assert_eq!(error.to_string(), cloned.to_string());
183    }
184
185    #[test]
186    fn test_result_type_ok() {
187        let result: DomainResult<i32> = Ok(42);
188        assert!(result.is_ok());
189        assert_eq!(result.unwrap(), 42);
190    }
191
192    #[test]
193    fn test_result_type_err() {
194        let result: DomainResult<i32> = Err(DomainError::agent_not_found("web"));
195        assert!(result.is_err());
196    }
197}