Skip to main content

the_code_graph_domain/
error.rs

1use std::path::PathBuf;
2
3#[derive(Debug, thiserror::Error)]
4pub enum CodeGraphError {
5    #[error("parse error in {file}: {message}")]
6    Parse { file: PathBuf, message: String },
7    #[error("resolution error: {0}")]
8    Resolution(String),
9    #[error("storage error: {0}")]
10    Storage(String),
11    #[error("git error: {0}")]
12    Git(String),
13    #[error("file system error: {path}: {source}")]
14    FileSystem {
15        path: PathBuf,
16        source: std::io::Error,
17    },
18    #[error("no project found (no .git directory)")]
19    NoProject,
20    #[error("refused to index blocklisted root: {0}")]
21    BlocklistedRoot(PathBuf),
22    #[error("index not built — run `code-graph index` first")]
23    IndexNotBuilt,
24    #[error("{0}")]
25    Other(String),
26}
27
28pub type Result<T> = std::result::Result<T, CodeGraphError>;
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use std::path::PathBuf;
34
35    #[test]
36    fn parse_error_display_contains_file_and_message() {
37        let err = CodeGraphError::Parse {
38            file: PathBuf::from("src/main.rs"),
39            message: "unexpected token".into(),
40        };
41        let msg = format!("{err}");
42        assert!(msg.contains("src/main.rs"), "missing file in: {msg}");
43        assert!(
44            msg.contains("unexpected token"),
45            "missing message in: {msg}"
46        );
47    }
48
49    #[test]
50    fn filesystem_error_display_contains_path() {
51        let err = CodeGraphError::FileSystem {
52            path: PathBuf::from("/some/path"),
53            source: std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
54        };
55        let msg = format!("{err}");
56        assert!(msg.contains("/some/path"), "missing path in: {msg}");
57    }
58
59    #[test]
60    fn all_variants_display_nonempty() {
61        let errors = vec![
62            CodeGraphError::Parse {
63                file: "f".into(),
64                message: "m".into(),
65            },
66            CodeGraphError::Resolution("r".into()),
67            CodeGraphError::Storage("s".into()),
68            CodeGraphError::Git("g".into()),
69            CodeGraphError::FileSystem {
70                path: "p".into(),
71                source: std::io::Error::new(std::io::ErrorKind::Other, "e"),
72            },
73            CodeGraphError::NoProject,
74            CodeGraphError::BlocklistedRoot("/".into()),
75            CodeGraphError::IndexNotBuilt,
76            CodeGraphError::Other("o".into()),
77        ];
78        for err in &errors {
79            assert!(!format!("{err}").is_empty(), "empty display for {err:?}");
80        }
81    }
82}