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 { collection: String, vector_id: String },
17
18 Validation { message: String },
20
21 Network { message: String },
23
24 Server { message: String },
26
27 Timeout { timeout_secs: u64 },
29
30 RateLimit { message: String },
32
33 Configuration { message: String },
35
36 Embedding { message: String },
38
39 Search { message: String },
41
42 Storage { message: String },
44
45 BatchOperation { message: String },
47
48 Mcp { message: String },
50
51
52 Serialization(String),
54
55 Http(reqwest::Error),
57
58 Io(std::io::Error),
60}
61
62impl VectorizerError {
63 pub fn authentication(message: impl Into<String>) -> Self {
65 Self::Authentication {
66 message: message.into(),
67 }
68 }
69
70 pub fn collection_not_found(collection: impl Into<String>) -> Self {
72 Self::CollectionNotFound {
73 collection: collection.into(),
74 }
75 }
76
77 pub fn vector_not_found(collection: impl Into<String>, vector_id: impl Into<String>) -> Self {
79 Self::VectorNotFound {
80 collection: collection.into(),
81 vector_id: vector_id.into(),
82 }
83 }
84
85 pub fn validation(message: impl Into<String>) -> Self {
87 Self::Validation {
88 message: message.into(),
89 }
90 }
91
92 pub fn network(message: impl Into<String>) -> Self {
94 Self::Network {
95 message: message.into(),
96 }
97 }
98
99 pub fn server(message: impl Into<String>) -> Self {
101 Self::Server {
102 message: message.into(),
103 }
104 }
105
106 pub fn timeout(timeout_secs: u64) -> Self {
108 Self::Timeout { timeout_secs }
109 }
110
111 pub fn rate_limit(message: impl Into<String>) -> Self {
113 Self::RateLimit {
114 message: message.into(),
115 }
116 }
117
118 pub fn configuration(message: impl Into<String>) -> Self {
120 Self::Configuration {
121 message: message.into(),
122 }
123 }
124
125 pub fn embedding(message: impl Into<String>) -> Self {
127 Self::Embedding {
128 message: message.into(),
129 }
130 }
131
132 pub fn search(message: impl Into<String>) -> Self {
134 Self::Search {
135 message: message.into(),
136 }
137 }
138
139 pub fn storage(message: impl Into<String>) -> Self {
141 Self::Storage {
142 message: message.into(),
143 }
144 }
145
146 pub fn batch_operation(message: impl Into<String>) -> Self {
148 Self::BatchOperation {
149 message: message.into(),
150 }
151 }
152
153 pub fn mcp(message: impl Into<String>) -> Self {
155 Self::Mcp {
156 message: message.into(),
157 }
158 }
159
160}
161
162impl std::fmt::Display for VectorizerError {
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164 match self {
165 VectorizerError::Authentication { message } => write!(f, "Authentication failed: {}", message),
166 VectorizerError::CollectionNotFound { collection } => write!(f, "Collection '{}' not found", collection),
167 VectorizerError::VectorNotFound { collection, vector_id } => write!(f, "Vector '{}' not found in collection '{}'", vector_id, collection),
168 VectorizerError::Validation { message } => write!(f, "Validation error: {}", message),
169 VectorizerError::Network { message } => write!(f, "Network error: {}", message),
170 VectorizerError::Server { message } => write!(f, "Server error: {}", message),
171 VectorizerError::Timeout { timeout_secs } => write!(f, "Request timeout after {}s", timeout_secs),
172 VectorizerError::RateLimit { message } => write!(f, "Rate limit exceeded: {}", message),
173 VectorizerError::Configuration { message } => write!(f, "Configuration error: {}", message),
174 VectorizerError::Embedding { message } => write!(f, "Embedding generation failed: {}", message),
175 VectorizerError::Search { message } => write!(f, "Search failed: {}", message),
176 VectorizerError::Storage { message } => write!(f, "Storage error: {}", message),
177 VectorizerError::BatchOperation { message } => write!(f, "Batch operation failed: {}", message),
178 VectorizerError::Mcp { message } => write!(f, "MCP error: {}", message),
179 VectorizerError::Serialization(message) => write!(f, "Serialization error: {}", message),
180 VectorizerError::Http(err) => write!(f, "HTTP error: {}", err),
181 VectorizerError::Io(err) => write!(f, "IO error: {}", err),
182 }
183 }
184}
185
186impl std::error::Error for VectorizerError {}
187
188impl From<serde_json::Error> for VectorizerError {
189 fn from(err: serde_json::Error) -> Self {
190 VectorizerError::Serialization(err.to_string())
191 }
192}
193
194impl From<std::io::Error> for VectorizerError {
195 fn from(err: std::io::Error) -> Self {
196 VectorizerError::Io(err)
197 }
198}
199
200impl From<reqwest::Error> for VectorizerError {
201 fn from(err: reqwest::Error) -> Self {
202 VectorizerError::Http(err)
203 }
204}
205
206pub fn map_http_error(status: u16, message: Option<String>) -> VectorizerError {
208 let default_message = format!("HTTP {}", status);
209 let message = message.unwrap_or(default_message);
210
211 match status {
212 401 => VectorizerError::authentication(message),
213 403 => VectorizerError::authentication("Access forbidden"),
214 404 => VectorizerError::server("Resource not found"),
215 429 => VectorizerError::rate_limit(message),
216 500..=599 => VectorizerError::server(message),
217 _ => VectorizerError::server(message),
218 }
219}