Skip to main content

relay_knowledge/code/error/
mod.rs

1//! Defines blocking code-index boundary failures and I/O conversion.
2
3use std::{error::Error, fmt};
4
5const INCREMENTAL_CHANGED_PATH_LIMIT_PREFIX: &str =
6    "incremental Git diff changed-path budget exceeded:";
7const GITLINK_EXPANSION_LIMIT_PREFIX: &str = "gitlink path ";
8
9/// Blocking code index failure.
10#[derive(Debug)]
11pub enum CodeIndexError {
12    Io(std::io::Error),
13    Git { args: Vec<String>, message: String },
14    TreeSitter(String),
15    InvalidInput(String),
16    Invariant(String),
17}
18
19impl fmt::Display for CodeIndexError {
20    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::Io(error) => write!(formatter, "code index I/O failed: {error}"),
23            Self::Git { args, message } => {
24                write!(formatter, "git command failed ({args:?}): {message}")
25            }
26            Self::TreeSitter(message) => write!(formatter, "tree-sitter parse failed: {message}"),
27            Self::InvalidInput(message) => write!(formatter, "invalid code index input: {message}"),
28            Self::Invariant(message) => write!(formatter, "code index invariant failed: {message}"),
29        }
30    }
31}
32
33impl Error for CodeIndexError {}
34
35impl CodeIndexError {
36    pub(in crate::code) fn incremental_changed_path_limit(observed: usize, limit: usize) -> Self {
37        Self::InvalidInput(format!(
38            "{INCREMENTAL_CHANGED_PATH_LIMIT_PREFIX} reached {observed} changed paths, exceeding the bounded limit of {limit}; run a full code index"
39        ))
40    }
41
42    pub(in crate::code) fn is_incremental_changed_path_limit(&self) -> bool {
43        matches!(self, Self::InvalidInput(message) if message.starts_with(INCREMENTAL_CHANGED_PATH_LIMIT_PREFIX))
44    }
45
46    pub(in crate::code) fn is_gitlink_expansion_limit(&self) -> bool {
47        matches!(self, Self::InvalidInput(message)
48            if message.starts_with(GITLINK_EXPANSION_LIMIT_PREFIX)
49                && message.contains(" expands to "))
50    }
51}
52
53impl From<std::io::Error> for CodeIndexError {
54    fn from(error: std::io::Error) -> Self {
55        Self::Io(error)
56    }
57}