helios_engine/
error.rs

1//! # Error and Result Module
2//!
3//! This module defines the custom error type `HeliosError` for the Helios Engine,
4//! and a convenient `Result` type alias.
5
6use thiserror::Error;
7
8/// The custom error type for the Helios Engine.
9#[derive(Error, Debug)]
10pub enum HeliosError {
11    /// An error related to configuration.
12    #[error("Configuration error: {0}")]
13    ConfigError(String),
14
15    /// An error related to the Language Model (LLM).
16    #[error("LLM error: {0}")]
17    LLMError(String),
18
19    /// An error related to a tool.
20    #[error("Tool error: {0}")]
21    ToolError(String),
22
23    /// An error related to an agent.
24    #[error("Agent error: {0}")]
25    AgentError(String),
26
27    /// An error related to a network request.
28    #[error("Network error: {0}")]
29    NetworkError(#[from] reqwest::Error),
30
31    /// An error related to serialization or deserialization.
32    #[error("Serialization error: {0}")]
33    SerializationError(#[from] serde_json::Error),
34
35    /// An I/O error.
36    #[error("IO error: {0}")]
37    IoError(#[from] std::io::Error),
38
39    /// An error related to parsing TOML.
40    #[error("TOML parsing error: {0}")]
41    TomlError(#[from] toml::de::Error),
42
43    /// An error from the Llama C++ backend.
44    #[cfg(feature = "local")]
45    #[error("Llama C++ error: {0}")]
46    LlamaCppError(String),
47
48    /// An error from the Candle backend.
49    #[cfg(feature = "candle")]
50    #[error("Candle error: {0}")]
51    CandleError(#[from] candle_core::Error),
52}
53
54/// A convenient `Result` type alias for the Helios Engine.
55pub type Result<T> = std::result::Result<T, HeliosError>;
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    /// Tests that the error types can be created.
62    #[test]
63    fn test_error_types() {
64        let config_error = HeliosError::ConfigError("Config issue".to_string());
65        assert!(matches!(config_error, HeliosError::ConfigError(_)));
66
67        let llm_error = HeliosError::LLMError("LLM issue".to_string());
68        assert!(matches!(llm_error, HeliosError::LLMError(_)));
69
70        let tool_error = HeliosError::ToolError("Tool issue".to_string());
71        assert!(matches!(tool_error, HeliosError::ToolError(_)));
72
73        let agent_error = HeliosError::AgentError("Agent issue".to_string());
74        assert!(matches!(agent_error, HeliosError::AgentError(_)));
75    }
76
77    /// Tests the display formatting of the error types.
78    #[test]
79    fn test_error_display() {
80        let config_error = HeliosError::ConfigError("Config issue".to_string());
81        assert_eq!(
82            format!("{}", config_error),
83            "Configuration error: Config issue"
84        );
85
86        let llm_error = HeliosError::LLMError("LLM issue".to_string());
87        assert_eq!(format!("{}", llm_error), "LLM error: LLM issue");
88
89        let tool_error = HeliosError::ToolError("Tool issue".to_string());
90        assert_eq!(format!("{}", tool_error), "Tool error: Tool issue");
91
92        let agent_error = HeliosError::AgentError("Agent issue".to_string());
93        assert_eq!(format!("{}", agent_error), "Agent error: Agent issue");
94    }
95}