vectorizer_sdk/
error.rs

1//! Error types for the Vectorizer SDK
2
3/// Result type alias for the Vectorizer SDK
4pub type Result<T> = std::result::Result<T, VectorizerError>;
5
6/// Main error type for the Vectorizer SDK
7#[derive(Debug)]
8pub enum VectorizerError {
9    /// Authentication failed
10    Authentication { message: String },
11
12    /// Collection not found
13    CollectionNotFound { collection: String },
14
15    /// Vector not found
16    VectorNotFound {
17        collection: String,
18        vector_id: String,
19    },
20
21    /// Validation error
22    Validation { message: String },
23
24    /// Network error
25    Network { message: String },
26
27    /// Server error
28    Server { message: String },
29
30    /// Timeout error
31    Timeout { timeout_secs: u64 },
32
33    /// Rate limit exceeded
34    RateLimit { message: String },
35
36    /// Configuration error
37    Configuration { message: String },
38
39    /// Embedding generation error
40    Embedding { message: String },
41
42    /// Search error
43    Search { message: String },
44
45    /// Storage error
46    Storage { message: String },
47
48    /// Batch operation error
49    BatchOperation { message: String },
50
51    /// MCP (Model Context Protocol) error
52    Mcp { message: String },
53
54    /// Serialization error
55    Serialization(String),
56
57    /// HTTP error
58    Http(reqwest::Error),
59
60    /// IO error
61    Io(std::io::Error),
62}
63
64impl VectorizerError {
65    /// Create a new authentication error
66    pub fn authentication(message: impl Into<String>) -> Self {
67        Self::Authentication {
68            message: message.into(),
69        }
70    }
71
72    /// Create a new collection not found error
73    pub fn collection_not_found(collection: impl Into<String>) -> Self {
74        Self::CollectionNotFound {
75            collection: collection.into(),
76        }
77    }
78
79    /// Create a new vector not found error
80    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    /// Create a new validation error
88    pub fn validation(message: impl Into<String>) -> Self {
89        Self::Validation {
90            message: message.into(),
91        }
92    }
93
94    /// Create a new network error
95    pub fn network(message: impl Into<String>) -> Self {
96        Self::Network {
97            message: message.into(),
98        }
99    }
100
101    /// Create a new server error
102    pub fn server(message: impl Into<String>) -> Self {
103        Self::Server {
104            message: message.into(),
105        }
106    }
107
108    /// Create a new timeout error
109    pub fn timeout(timeout_secs: u64) -> Self {
110        Self::Timeout { timeout_secs }
111    }
112
113    /// Create a new rate limit error
114    pub fn rate_limit(message: impl Into<String>) -> Self {
115        Self::RateLimit {
116            message: message.into(),
117        }
118    }
119
120    /// Create a new configuration error
121    pub fn configuration(message: impl Into<String>) -> Self {
122        Self::Configuration {
123            message: message.into(),
124        }
125    }
126
127    /// Create a new embedding error
128    pub fn embedding(message: impl Into<String>) -> Self {
129        Self::Embedding {
130            message: message.into(),
131        }
132    }
133
134    /// Create a new search error
135    pub fn search(message: impl Into<String>) -> Self {
136        Self::Search {
137            message: message.into(),
138        }
139    }
140
141    /// Create a new storage error
142    pub fn storage(message: impl Into<String>) -> Self {
143        Self::Storage {
144            message: message.into(),
145        }
146    }
147
148    /// Create a new batch operation error
149    pub fn batch_operation(message: impl Into<String>) -> Self {
150        Self::BatchOperation {
151            message: message.into(),
152        }
153    }
154
155    /// Create a new MCP error
156    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
228/// Map HTTP status codes to appropriate VectorizerError variants
229pub 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}