agentroot_core/
error.rs

1//! Error types for agentroot
2
3use thiserror::Error;
4
5/// Result type alias using AgentRootError
6pub type Result<T> = std::result::Result<T, AgentRootError>;
7
8/// Error type alias for convenience
9pub type Error = AgentRootError;
10
11/// Exit codes for CLI
12pub mod exit_codes {
13    pub const SUCCESS: i32 = 0;
14    pub const GENERAL_ERROR: i32 = 1;
15    pub const NOT_FOUND: i32 = 2;
16    pub const INVALID_INPUT: i32 = 3;
17}
18
19/// Main error type for agentroot
20#[derive(Debug, Error)]
21pub enum AgentRootError {
22    #[error("Database error: {0}")]
23    Database(#[from] rusqlite::Error),
24
25    #[error("IO error: {0}")]
26    Io(#[from] std::io::Error),
27
28    #[error("Walk directory error: {0}")]
29    WalkDir(#[from] walkdir::Error),
30
31    #[error("Collection not found: {0}")]
32    CollectionNotFound(String),
33
34    #[error("Document not found: {0}")]
35    DocumentNotFound(String),
36
37    #[error("Invalid virtual path: {0}")]
38    InvalidVirtualPath(String),
39
40    #[error("LLM error: {0}")]
41    Llm(String),
42
43    #[error("Model not found: {0}")]
44    ModelNotFound(String),
45
46    #[error("Configuration error: {0}")]
47    Config(String),
48
49    #[error("Index error: {0}")]
50    Index(String),
51
52    #[error("Parse error: {0}")]
53    Parse(String),
54
55    #[error("Search error: {0}")]
56    Search(String),
57
58    #[error("Serialization error: {0}")]
59    Serialization(#[from] serde_json::Error),
60
61    #[error("YAML error: {0}")]
62    Yaml(#[from] serde_yaml::Error),
63
64    #[error("HTTP error: {0}")]
65    Http(#[from] reqwest::Error),
66
67    #[error("Regex error: {0}")]
68    Regex(#[from] regex::Error),
69
70    #[error("Glob pattern error: {0}")]
71    GlobPattern(#[from] glob::PatternError),
72
73    #[error("CSV error: {0}")]
74    Csv(String),
75
76    #[error("Invalid input: {0}")]
77    InvalidInput(String),
78
79    #[error("External service error: {0}")]
80    ExternalError(String),
81
82    #[error("{0}")]
83    Other(#[from] anyhow::Error),
84}
85
86impl AgentRootError {
87    /// Get the exit code for this error
88    pub fn exit_code(&self) -> i32 {
89        match self {
90            Self::CollectionNotFound(_) | Self::DocumentNotFound(_) => exit_codes::NOT_FOUND,
91            Self::InvalidVirtualPath(_) | Self::Config(_) => exit_codes::INVALID_INPUT,
92            _ => exit_codes::GENERAL_ERROR,
93        }
94    }
95}
96
97impl From<csv::Error> for AgentRootError {
98    fn from(err: csv::Error) -> Self {
99        Self::Csv(err.to_string())
100    }
101}