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