context_creator/utils/
error.rs

1//! Error types for context-creator
2
3use thiserror::Error;
4
5/// Main error type for context-creator operations
6#[derive(Error, Debug)]
7pub enum ContextCreatorError {
8    /// File system related errors
9    #[error("Invalid path: {0}")]
10    InvalidPath(String),
11
12    #[error("Failed to read file: {0}")]
13    ReadError(String),
14
15    #[error("Failed to write file: {0}")]
16    WriteError(String),
17
18    /// Configuration errors
19    #[error("Invalid configuration: {0}")]
20    InvalidConfiguration(String),
21
22    #[error("Failed to parse configuration: {0}")]
23    ConfigParseError(String),
24
25    #[error("Configuration file error: {0}")]
26    ConfigError(String),
27
28    /// Processing errors
29    #[error("Token counting error: {0}")]
30    TokenCountError(String),
31
32    #[error("Context generation error: {0}")]
33    ContextGenerationError(String),
34
35    #[error("File prioritization error: {0}")]
36    PrioritizationError(String),
37
38    /// External tool errors
39    #[error("{tool} not found. {install_instructions}")]
40    LlmToolNotFound {
41        tool: String,
42        install_instructions: String,
43    },
44
45    #[error("Subprocess error: {0}")]
46    SubprocessError(String),
47
48    /// Resource limits
49    #[error("File too large: {0} (max: {1} bytes)")]
50    FileTooLarge(String, usize),
51
52    #[error("Token limit exceeded: {current} tokens (max: {max})")]
53    TokenLimitExceeded { current: usize, max: usize },
54
55    /// Parsing errors
56    #[error("Parse error: {0}")]
57    ParseError(String),
58
59    /// Pattern matching errors
60    #[error("Invalid glob pattern: {0}")]
61    InvalidGlobPattern(String),
62
63    /// Mutex errors
64    #[error("Mutex was poisoned, indicating a previous panic")]
65    MutexPoisoned,
66
67    /// Security errors
68    #[error("Security error: {0}")]
69    SecurityError(String),
70
71    /// Remote repository errors
72    #[error("Remote fetch error: {0}")]
73    RemoteFetchError(String),
74
75    /// Clipboard errors
76    #[error("Clipboard error: {0}")]
77    ClipboardError(String),
78
79    /// Parallel processing errors
80    #[error("File processing error for {path}: {error}")]
81    FileProcessingError { path: String, error: String },
82
83    #[error("Token counting error for {path}: {error}")]
84    TokenCountingError { path: String, error: String },
85
86    #[error("Parallel processing errors: {error_count} files failed")]
87    ParallelProcessingErrors { error_count: usize },
88
89    /// General I/O errors
90    #[error("I/O error: {0}")]
91    IoError(#[from] std::io::Error),
92
93    /// UTF-8 conversion errors
94    #[error("UTF-8 conversion error: {0}")]
95    Utf8Error(#[from] std::string::FromUtf8Error),
96}
97
98/// Result type alias for context-creator operations
99pub type Result<T> = std::result::Result<T, ContextCreatorError>;
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_error_display() {
107        let err = ContextCreatorError::InvalidPath("/invalid/path".to_string());
108        assert_eq!(err.to_string(), "Invalid path: /invalid/path");
109
110        let err = ContextCreatorError::TokenLimitExceeded {
111            current: 200000,
112            max: 150000,
113        };
114        assert_eq!(
115            err.to_string(),
116            "Token limit exceeded: 200000 tokens (max: 150000)"
117        );
118    }
119
120    #[test]
121    fn test_io_error_conversion() {
122        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
123        let err: ContextCreatorError = io_err.into();
124        assert!(matches!(err, ContextCreatorError::IoError(_)));
125    }
126}