aurora_semantic/
error.rs

1//! Error types for the aurora-semantic crate.
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6use crate::types::WorkspaceId;
7
8/// Main error type for the aurora-semantic crate.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Configuration error.
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// Workspace not found.
16    #[error("Workspace not found: {0}")]
17    WorkspaceNotFound(WorkspaceId),
18
19    /// Workspace already exists.
20    #[error("Workspace already exists: {0}")]
21    WorkspaceExists(WorkspaceId),
22
23    /// File not found.
24    #[error("File not found: {0}")]
25    FileNotFound(PathBuf),
26
27    /// Directory not found.
28    #[error("Directory not found: {0}")]
29    DirectoryNotFound(PathBuf),
30
31    /// IO error.
32    #[error("IO error: {0}")]
33    Io(#[from] std::io::Error),
34
35    /// Parsing error.
36    #[error("Parsing error in {file}: {message}")]
37    Parse {
38        /// File that failed to parse.
39        file: PathBuf,
40        /// Error message describing the parse failure.
41        message: String,
42    },
43
44    /// Index error.
45    #[error("Index error: {0}")]
46    Index(String),
47
48    /// Search error.
49    #[error("Search error: {0}")]
50    Search(String),
51
52    /// Embedding error.
53    #[error("Embedding error: {0}")]
54    Embedding(String),
55
56    /// Storage error.
57    #[error("Storage error: {0}")]
58    Storage(String),
59
60    /// Serialization error.
61    #[error("Serialization error: {0}")]
62    Serialization(String),
63
64    /// Tantivy error.
65    #[error("Tantivy error: {0}")]
66    Tantivy(#[from] tantivy::TantivyError),
67
68    /// Tantivy query parser error.
69    #[error("Query parser error: {0}")]
70    QueryParser(#[from] tantivy::query::QueryParserError),
71
72    /// Model loading error.
73    #[error("Model loading error: {0}")]
74    ModelLoad(String),
75
76    /// Invalid query.
77    #[error("Invalid query: {0}")]
78    InvalidQuery(String),
79
80    /// Operation cancelled.
81    #[error("Operation cancelled")]
82    Cancelled,
83
84    /// Internal error.
85    #[error("Internal error: {0}")]
86    Internal(String),
87}
88
89/// Result type alias using our Error.
90pub type Result<T> = std::result::Result<T, Error>;
91
92impl Error {
93    /// Create a configuration error.
94    pub fn config(msg: impl Into<String>) -> Self {
95        Self::Config(msg.into())
96    }
97
98    /// Create a parsing error.
99    pub fn parse(file: impl Into<PathBuf>, msg: impl Into<String>) -> Self {
100        Self::Parse {
101            file: file.into(),
102            message: msg.into(),
103        }
104    }
105
106    /// Create an index error.
107    pub fn index(msg: impl Into<String>) -> Self {
108        Self::Index(msg.into())
109    }
110
111    /// Create a search error.
112    pub fn search(msg: impl Into<String>) -> Self {
113        Self::Search(msg.into())
114    }
115
116    /// Create an embedding error.
117    pub fn embedding(msg: impl Into<String>) -> Self {
118        Self::Embedding(msg.into())
119    }
120
121    /// Create a storage error.
122    pub fn storage(msg: impl Into<String>) -> Self {
123        Self::Storage(msg.into())
124    }
125
126    /// Create a serialization error.
127    pub fn serialization(msg: impl Into<String>) -> Self {
128        Self::Serialization(msg.into())
129    }
130
131    /// Create a model loading error.
132    pub fn model_load(msg: impl Into<String>) -> Self {
133        Self::ModelLoad(msg.into())
134    }
135
136    /// Create an invalid query error.
137    pub fn invalid_query(msg: impl Into<String>) -> Self {
138        Self::InvalidQuery(msg.into())
139    }
140
141    /// Create an internal error.
142    pub fn internal(msg: impl Into<String>) -> Self {
143        Self::Internal(msg.into())
144    }
145
146    /// Check if this error is recoverable.
147    pub fn is_recoverable(&self) -> bool {
148        matches!(
149            self,
150            Self::FileNotFound(_)
151                | Self::Parse { .. }
152                | Self::InvalidQuery(_)
153                | Self::Cancelled
154        )
155    }
156
157    /// Check if this error should trigger a retry.
158    pub fn is_retryable(&self) -> bool {
159        matches!(self, Self::Io(_) | Self::Storage(_))
160    }
161}
162
163impl From<serde_json::Error> for Error {
164    fn from(e: serde_json::Error) -> Self {
165        Self::Serialization(e.to_string())
166    }
167}
168
169impl From<bincode::Error> for Error {
170    fn from(e: bincode::Error) -> Self {
171        Self::Serialization(e.to_string())
172    }
173}