1pub type Result<T> = std::result::Result<T, VectorizerError>;
5
6#[derive(Debug)]
8pub enum VectorizerError {
9 Authentication { message: String },
11
12 CollectionNotFound { collection: String },
14
15 VectorNotFound {
17 collection: String,
18 vector_id: String,
19 },
20
21 Validation { message: String },
23
24 Network { message: String },
26
27 Server { message: String },
29
30 Timeout { timeout_secs: u64 },
32
33 RateLimit { message: String },
35
36 Configuration { message: String },
38
39 Embedding { message: String },
41
42 Search { message: String },
44
45 Storage { message: String },
47
48 BatchOperation { message: String },
50
51 Mcp { message: String },
53
54 Serialization(String),
56
57 Http(reqwest::Error),
59
60 Io(std::io::Error),
62}
63
64impl VectorizerError {
65 pub fn authentication(message: impl Into<String>) -> Self {
67 Self::Authentication {
68 message: message.into(),
69 }
70 }
71
72 pub fn collection_not_found(collection: impl Into<String>) -> Self {
74 Self::CollectionNotFound {
75 collection: collection.into(),
76 }
77 }
78
79 pub fn vector_not_found(collection: impl Into<String>, vector_id: impl Into<String>) -> Self {
81 Self::VectorNotFound {
82 collection: collection.into(),
83 vector_id: vector_id.into(),
84 }
85 }
86
87 pub fn validation(message: impl Into<String>) -> Self {
89 Self::Validation {
90 message: message.into(),
91 }
92 }
93
94 pub fn network(message: impl Into<String>) -> Self {
96 Self::Network {
97 message: message.into(),
98 }
99 }
100
101 pub fn server(message: impl Into<String>) -> Self {
103 Self::Server {
104 message: message.into(),
105 }
106 }
107
108 pub fn timeout(timeout_secs: u64) -> Self {
110 Self::Timeout { timeout_secs }
111 }
112
113 pub fn rate_limit(message: impl Into<String>) -> Self {
115 Self::RateLimit {
116 message: message.into(),
117 }
118 }
119
120 pub fn configuration(message: impl Into<String>) -> Self {
122 Self::Configuration {
123 message: message.into(),
124 }
125 }
126
127 pub fn embedding(message: impl Into<String>) -> Self {
129 Self::Embedding {
130 message: message.into(),
131 }
132 }
133
134 pub fn search(message: impl Into<String>) -> Self {
136 Self::Search {
137 message: message.into(),
138 }
139 }
140
141 pub fn storage(message: impl Into<String>) -> Self {
143 Self::Storage {
144 message: message.into(),
145 }
146 }
147
148 pub fn batch_operation(message: impl Into<String>) -> Self {
150 Self::BatchOperation {
151 message: message.into(),
152 }
153 }
154
155 pub fn mcp(message: impl Into<String>) -> Self {
157 Self::Mcp {
158 message: message.into(),
159 }
160 }
161}
162
163impl std::fmt::Display for VectorizerError {
164 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165 match self {
166 VectorizerError::Authentication { message } => {
167 write!(f, "Authentication failed: {}", message)
168 }
169 VectorizerError::CollectionNotFound { collection } => {
170 write!(f, "Collection '{}' not found", collection)
171 }
172 VectorizerError::VectorNotFound {
173 collection,
174 vector_id,
175 } => write!(
176 f,
177 "Vector '{}' not found in collection '{}'",
178 vector_id, collection
179 ),
180 VectorizerError::Validation { message } => write!(f, "Validation error: {}", message),
181 VectorizerError::Network { message } => write!(f, "Network error: {}", message),
182 VectorizerError::Server { message } => write!(f, "Server error: {}", message),
183 VectorizerError::Timeout { timeout_secs } => {
184 write!(f, "Request timeout after {}s", timeout_secs)
185 }
186 VectorizerError::RateLimit { message } => write!(f, "Rate limit exceeded: {}", message),
187 VectorizerError::Configuration { message } => {
188 write!(f, "Configuration error: {}", message)
189 }
190 VectorizerError::Embedding { message } => {
191 write!(f, "Embedding generation failed: {}", message)
192 }
193 VectorizerError::Search { message } => write!(f, "Search failed: {}", message),
194 VectorizerError::Storage { message } => write!(f, "Storage error: {}", message),
195 VectorizerError::BatchOperation { message } => {
196 write!(f, "Batch operation failed: {}", message)
197 }
198 VectorizerError::Mcp { message } => write!(f, "MCP error: {}", message),
199 VectorizerError::Serialization(message) => {
200 write!(f, "Serialization error: {}", message)
201 }
202 VectorizerError::Http(err) => write!(f, "HTTP error: {}", err),
203 VectorizerError::Io(err) => write!(f, "IO error: {}", err),
204 }
205 }
206}
207
208impl std::error::Error for VectorizerError {}
209
210impl From<serde_json::Error> for VectorizerError {
211 fn from(err: serde_json::Error) -> Self {
212 VectorizerError::Serialization(err.to_string())
213 }
214}
215
216impl From<std::io::Error> for VectorizerError {
217 fn from(err: std::io::Error) -> Self {
218 VectorizerError::Io(err)
219 }
220}
221
222impl From<reqwest::Error> for VectorizerError {
223 fn from(err: reqwest::Error) -> Self {
224 VectorizerError::Http(err)
225 }
226}
227
228pub fn map_http_error(status: u16, message: Option<String>) -> VectorizerError {
230 let default_message = format!("HTTP {}", status);
231 let message = message.unwrap_or(default_message);
232
233 match status {
234 401 => VectorizerError::authentication(message),
235 403 => VectorizerError::authentication("Access forbidden"),
236 404 => VectorizerError::server("Resource not found"),
237 429 => VectorizerError::rate_limit(message),
238 500..=599 => VectorizerError::server(message),
239 _ => VectorizerError::server(message),
240 }
241}