codeprism_lang_js/
error.rs

1//! Error types for JavaScript/TypeScript parser
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Result type alias for JavaScript parser operations
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// JavaScript/TypeScript parser error type
10#[derive(Debug, Error)]
11pub enum Error {
12    /// Tree-sitter parsing error
13    #[error("Parse error in {file}: {message}")]
14    Parse {
15        /// File that failed to parse
16        file: PathBuf,
17        /// Error message
18        message: String,
19    },
20
21    /// Tree-sitter language error
22    #[error("Failed to set language: {0}")]
23    Language(String),
24
25    /// Node extraction error
26    #[error("Failed to extract node at {file}:{line}:{column}: {message}")]
27    NodeExtraction {
28        /// File path
29        file: PathBuf,
30        /// Line number
31        line: usize,
32        /// Column number
33        column: usize,
34        /// Error message
35        message: String,
36    },
37
38    /// UTF-8 conversion error
39    #[error("UTF-8 conversion error: {0}")]
40    Utf8(#[from] std::str::Utf8Error),
41
42    /// IO error
43    #[error("IO error: {0}")]
44    Io(#[from] std::io::Error),
45
46    /// Other error
47    #[error("{0}")]
48    Other(String),
49}
50
51impl Error {
52    /// Create a parse error
53    pub fn parse(file: impl Into<PathBuf>, message: impl Into<String>) -> Self {
54        Self::Parse {
55            file: file.into(),
56            message: message.into(),
57        }
58    }
59
60    /// Create a node extraction error
61    pub fn node_extraction(
62        file: impl Into<PathBuf>,
63        line: usize,
64        column: usize,
65        message: impl Into<String>,
66    ) -> Self {
67        Self::NodeExtraction {
68            file: file.into(),
69            line,
70            column,
71            message: message.into(),
72        }
73    }
74
75    /// Create a language error
76    pub fn language(message: impl Into<String>) -> Self {
77        Self::Language(message.into())
78    }
79
80    /// Create an other error
81    pub fn other(message: impl Into<String>) -> Self {
82        Self::Other(message.into())
83    }
84}