Skip to main content

pathfinder_treesitter/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4/// Errors that the surgeon engine can produce.
5#[derive(Debug, Error)]
6pub enum SurgeonError {
7    /// The requested symbol could not be found via the semantic path.
8    #[error("symbol not found: {path}")]
9    SymbolNotFound {
10        path: String,
11        /// Alternative symbols with similar names (Levenshtein distance).
12        did_you_mean: Vec<String>,
13    },
14
15    /// The requested file does not exist on disk.
16    ///
17    /// Distinct from a generic I/O error so the MCP layer can surface this as a
18    /// client error (`INVALID_PARAMS / FILE_NOT_FOUND`) rather than an internal
19    /// server error (`INTERNAL_ERROR`).
20    #[error("file not found: {0}")]
21    FileNotFound(PathBuf),
22
23    /// The requested file's language is not supported.
24    #[error("unsupported language for path: {0}")]
25    UnsupportedLanguage(PathBuf),
26
27    /// A parsing error occurred.
28    #[error("parse error in {path}: {reason}")]
29    ParseError {
30        path: std::path::PathBuf,
31        reason: String,
32    },
33
34    /// A file-system error occurred when attempting to read source files.
35    #[error("filesystem error: {0}")]
36    Io(#[from] std::io::Error),
37}
38
39impl From<SurgeonError> for pathfinder_common::error::PathfinderError {
40    fn from(error: SurgeonError) -> Self {
41        match error {
42            SurgeonError::SymbolNotFound { path, did_you_mean } => Self::SymbolNotFound {
43                semantic_path: path,
44                did_you_mean,
45            },
46            SurgeonError::FileNotFound(path) => Self::FileNotFound { path },
47            SurgeonError::UnsupportedLanguage(path) => Self::UnsupportedLanguage { path },
48            SurgeonError::ParseError { path, reason } => Self::ParseError { path, reason },
49            SurgeonError::Io(err) => Self::IoError {
50                message: err.to_string(),
51            },
52        }
53    }
54}