1use std::path::PathBuf;
4use thiserror::Error;
5
6use crate::types::WorkspaceId;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("Configuration error: {0}")]
13 Config(String),
14
15 #[error("Workspace not found: {0}")]
17 WorkspaceNotFound(WorkspaceId),
18
19 #[error("Workspace already exists: {0}")]
21 WorkspaceExists(WorkspaceId),
22
23 #[error("File not found: {0}")]
25 FileNotFound(PathBuf),
26
27 #[error("Directory not found: {0}")]
29 DirectoryNotFound(PathBuf),
30
31 #[error("IO error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("Parsing error in {file}: {message}")]
37 Parse {
38 file: PathBuf,
40 message: String,
42 },
43
44 #[error("Index error: {0}")]
46 Index(String),
47
48 #[error("Search error: {0}")]
50 Search(String),
51
52 #[error("Embedding error: {0}")]
54 Embedding(String),
55
56 #[error("Storage error: {0}")]
58 Storage(String),
59
60 #[error("Serialization error: {0}")]
62 Serialization(String),
63
64 #[error("Tantivy error: {0}")]
66 Tantivy(#[from] tantivy::TantivyError),
67
68 #[error("Query parser error: {0}")]
70 QueryParser(#[from] tantivy::query::QueryParserError),
71
72 #[error("Model loading error: {0}")]
74 ModelLoad(String),
75
76 #[error("Invalid query: {0}")]
78 InvalidQuery(String),
79
80 #[error("Operation cancelled")]
82 Cancelled,
83
84 #[error("Internal error: {0}")]
86 Internal(String),
87}
88
89pub type Result<T> = std::result::Result<T, Error>;
91
92impl Error {
93 pub fn config(msg: impl Into<String>) -> Self {
95 Self::Config(msg.into())
96 }
97
98 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 pub fn index(msg: impl Into<String>) -> Self {
108 Self::Index(msg.into())
109 }
110
111 pub fn search(msg: impl Into<String>) -> Self {
113 Self::Search(msg.into())
114 }
115
116 pub fn embedding(msg: impl Into<String>) -> Self {
118 Self::Embedding(msg.into())
119 }
120
121 pub fn storage(msg: impl Into<String>) -> Self {
123 Self::Storage(msg.into())
124 }
125
126 pub fn serialization(msg: impl Into<String>) -> Self {
128 Self::Serialization(msg.into())
129 }
130
131 pub fn model_load(msg: impl Into<String>) -> Self {
133 Self::ModelLoad(msg.into())
134 }
135
136 pub fn invalid_query(msg: impl Into<String>) -> Self {
138 Self::InvalidQuery(msg.into())
139 }
140
141 pub fn internal(msg: impl Into<String>) -> Self {
143 Self::Internal(msg.into())
144 }
145
146 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 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}