Skip to main content

argus_core/
error.rs

1use std::path::PathBuf;
2
3use miette::Diagnostic;
4
5/// Errors that can occur across the Argus platform.
6///
7/// Each variant wraps a specific error domain. Library crates use this type
8/// directly; the binary crate converts to `miette::Report` at the boundary.
9///
10/// # Examples
11///
12/// ```
13/// use argus_core::ArgusError;
14///
15/// let err = ArgusError::Config("missing API key".into());
16/// assert!(err.to_string().contains("missing API key"));
17/// ```
18#[derive(Debug, thiserror::Error, Diagnostic)]
19pub enum ArgusError {
20    /// Filesystem I/O failure.
21    #[error("IO error: {0}")]
22    #[diagnostic(code(argus::io), help("Check file permissions and paths"))]
23    Io(#[from] std::io::Error),
24
25    /// Invalid or missing configuration.
26    #[error("Configuration error: {0}")]
27    #[diagnostic(
28        code(argus::config),
29        help("Run `argus init` to create a default config, or check .argus.toml syntax")
30    )]
31    Config(String),
32
33    /// Git operation failure.
34    #[error("Git error: {0}")]
35    #[diagnostic(code(argus::git), help("Make sure you're inside a git repository"))]
36    Git(String),
37
38    /// Source code parsing failure.
39    #[error("Parse error: {0}")]
40    #[diagnostic(
41        code(argus::parse),
42        help("Check that the file is valid source code in a supported language")
43    )]
44    Parse(String),
45
46    /// LLM API or response error.
47    #[error("LLM error: {0}")]
48    #[diagnostic(code(argus::llm), help("Check your API key and provider config in .argus.toml. Run `argus doctor` to diagnose."))]
49    Llm(String),
50
51    /// JSON serialization / deserialization failure.
52    #[error("Serialization error: {0}")]
53    #[diagnostic(code(argus::serde))]
54    Serialization(#[from] serde_json::Error),
55
56    /// TOML deserialization failure.
57    #[error("TOML parse error: {0}")]
58    #[diagnostic(
59        code(argus::toml),
60        help("Check .argus.toml syntax — run `argus init` to generate a fresh one")
61    )]
62    Toml(#[from] toml::de::Error),
63
64    /// A required file was not found.
65    #[error("File not found: {}", .0.display())]
66    #[diagnostic(
67        code(argus::file_not_found),
68        help("Check the path exists and is readable")
69    )]
70    FileNotFound(PathBuf),
71
72    /// Embedding API error.
73    #[error("Embedding error: {0}")]
74    #[diagnostic(
75        code(argus::embedding),
76        help("Check your embedding provider API key. Run `argus doctor` to diagnose.")
77    )]
78    Embedding(String),
79
80    /// Database operation failure.
81    #[error("Database error: {0}")]
82    #[diagnostic(
83        code(argus::database),
84        help("Try deleting .argus/index.db and re-indexing")
85    )]
86    Database(String),
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn io_error_converts() {
95        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "gone");
96        let err: ArgusError = io_err.into();
97        assert!(err.to_string().contains("gone"));
98    }
99
100    #[test]
101    fn config_error_displays_message() {
102        let err = ArgusError::Config("bad value".into());
103        assert_eq!(err.to_string(), "Configuration error: bad value");
104    }
105
106    #[test]
107    fn file_not_found_shows_path() {
108        let err = ArgusError::FileNotFound(PathBuf::from("/tmp/missing.rs"));
109        assert!(err.to_string().contains("/tmp/missing.rs"));
110    }
111}