Skip to main content

git_semantic/index/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum IndexError {
5    #[error("not a git repository: .git directory not found")]
6    NotAGitRepository,
7
8    #[error("invalid .git file format — unable to resolve worktree path")]
9    InvalidGitFile,
10
11    #[error("index not found — run 'git-semantic index' first")]
12    IndexNotFound,
13
14    #[error("index is corrupted or was created by an incompatible version")]
15    CorruptedIndex(#[source] bincode::Error),
16
17    #[error("failed to write index: disk may be full or permissions denied")]
18    SaveFailed(#[source] std::io::Error),
19
20    #[error(transparent)]
21    Io(#[from] std::io::Error),
22
23    #[error("failed to serialize/deserialize index")]
24    Bincode(#[from] bincode::Error),
25
26    #[error(transparent)]
27    Embedding(#[from] crate::embedding::EmbeddingError),
28}
29
30impl IndexError {
31    /// Returns a user-facing hint for how to resolve this error.
32    pub fn hint(&self) -> Option<&'static str> {
33        match self {
34            Self::NotAGitRepository => {
35                Some("Make sure you're inside a git repository, or use --path to specify one.")
36            }
37            Self::InvalidGitFile => {
38                Some("The .git file may be corrupted. This can happen with broken worktree setups.")
39            }
40            Self::IndexNotFound => Some("Run: git-semantic index"),
41            Self::CorruptedIndex(_) => {
42                Some("The index file is unreadable. Rebuild it with: git-semantic index --force")
43            }
44            Self::SaveFailed(_) => {
45                Some("Check available disk space and file permissions on the .git directory.")
46            }
47            Self::Io(_) => Some("Check file permissions and available disk space."),
48            Self::Bincode(_) => Some(
49                "The index may be from an incompatible version. Rebuild with: git-semantic index --force",
50            ),
51            Self::Embedding(_) => None, // Delegate to EmbeddingError's own hint
52        }
53    }
54
55    /// Returns an error code for programmatic identification.
56    pub fn code(&self) -> &'static str {
57        match self {
58            Self::NotAGitRepository => "E3001",
59            Self::InvalidGitFile => "E3002",
60            Self::IndexNotFound => "E3003",
61            Self::CorruptedIndex(_) => "E3004",
62            Self::SaveFailed(_) => "E3005",
63            Self::Io(_) => "E3006",
64            Self::Bincode(_) => "E3007",
65            Self::Embedding(_) => "E3008",
66        }
67    }
68}