1use thiserror::Error;
2
3#[derive(Error, Debug)]
5pub enum VearchError {
6 #[error("HTTP request failed: {0}")]
7 HttpError(#[from] reqwest::Error),
8
9 #[error("JSON serialization failed: {0}")]
10 SerializationError(#[from] serde_json::Error),
11
12 #[error("Invalid URL: {0}")]
13 InvalidUrl(#[from] url::ParseError),
14
15 #[error("Vearch API error: {code} - {message}")]
16 ApiError { code: i32, message: String },
17
18 #[error("Invalid parameter: {0}")]
19 InvalidParameter(String),
20
21 #[error("Operation failed: {0}")]
22 OperationFailed(String),
23
24 #[error("Document not found: {0}")]
25 DocumentNotFound(String),
26
27 #[error("Database not found: {0}")]
28 DatabaseNotFound(String),
29
30 #[error("Space not found: {0}")]
31 SpaceNotFound(String),
32
33 #[error("Vector dimension mismatch: expected {expected}, got {actual}")]
34 VectorDimensionMismatch { expected: usize, actual: usize },
35}
36
37impl From<serde_json::Value> for VearchError {
38 fn from(value: serde_json::Value) -> Self {
39 if let (Some(code), Some(msg)) = (
40 value.get("code").and_then(|v| v.as_i64()),
41 value.get("msg").and_then(|v| v.as_str()),
42 ) {
43 if code != 0 {
44 return VearchError::ApiError {
45 code: code as i32,
46 message: msg.to_string(),
47 };
48 }
49 }
50 VearchError::OperationFailed("Unknown API error".to_string())
51 }
52}
53
54pub type VearchResult<T> = Result<T, VearchError>;