codegraph_python/
error.rs

1use std::io;
2use std::path::PathBuf;
3use thiserror::Error;
4
5/// Result type alias for parser operations
6pub type Result<T> = std::result::Result<T, ParseError>;
7
8/// Errors that can occur during Python parsing
9#[derive(Error, Debug)]
10pub enum ParseError {
11    /// I/O error reading a file
12    #[error("Failed to read file {path}: {source}")]
13    IoError { path: PathBuf, source: io::Error },
14
15    /// File exceeds maximum size limit
16    #[error(
17        "File {path} exceeds maximum size limit of {max_size} bytes (actual: {actual_size} bytes)"
18    )]
19    FileTooLarge {
20        path: PathBuf,
21        max_size: usize,
22        actual_size: usize,
23    },
24
25    /// Python syntax error
26    #[error("Syntax error in {file} at line {line}, column {column}: {message}")]
27    SyntaxError {
28        file: String,
29        line: usize,
30        column: usize,
31        message: String,
32    },
33
34    /// Error from graph database operations
35    #[error("Graph operation failed: {0}")]
36    GraphError(String),
37
38    /// Invalid parser configuration
39    #[error("Invalid configuration: {0}")]
40    InvalidConfig(String),
41
42    /// Unsupported Python language feature
43    #[error("Unsupported Python feature in {file}: {feature}")]
44    UnsupportedFeature { file: String, feature: String },
45}
46
47impl ParseError {
48    /// Create an IoError from a path and io::Error
49    pub fn io_error(path: impl Into<PathBuf>, source: io::Error) -> Self {
50        ParseError::IoError {
51            path: path.into(),
52            source,
53        }
54    }
55
56    /// Create a FileTooLarge error
57    pub fn file_too_large(path: impl Into<PathBuf>, max_size: usize, actual_size: usize) -> Self {
58        ParseError::FileTooLarge {
59            path: path.into(),
60            max_size,
61            actual_size,
62        }
63    }
64
65    /// Create a SyntaxError
66    pub fn syntax_error(
67        file: impl Into<String>,
68        line: usize,
69        column: usize,
70        message: impl Into<String>,
71    ) -> Self {
72        ParseError::SyntaxError {
73            file: file.into(),
74            line,
75            column,
76            message: message.into(),
77        }
78    }
79
80    /// Create a GraphError
81    pub fn graph_error(message: impl Into<String>) -> Self {
82        ParseError::GraphError(message.into())
83    }
84
85    /// Create an InvalidConfig error
86    pub fn invalid_config(message: impl Into<String>) -> Self {
87        ParseError::InvalidConfig(message.into())
88    }
89
90    /// Create an UnsupportedFeature error
91    pub fn unsupported_feature(file: impl Into<String>, feature: impl Into<String>) -> Self {
92        ParseError::UnsupportedFeature {
93            file: file.into(),
94            feature: feature.into(),
95        }
96    }
97}