1use thiserror::Error;
18use tonic::Status;
19
20#[derive(Error, Debug)]
22pub enum GrpcError {
23 #[error("Index not found: {0}")]
24 IndexNotFound(String),
25
26 #[error("Index already exists: {0}")]
27 IndexAlreadyExists(String),
28
29 #[error("Invalid dimension: expected {expected}, got {actual}")]
30 DimensionMismatch { expected: usize, actual: usize },
31
32 #[error("Invalid request: {0}")]
33 InvalidRequest(String),
34
35 #[error("HNSW error: {0}")]
36 HnswError(String),
37
38 #[error("Internal error: {0}")]
39 Internal(String),
40}
41
42impl From<GrpcError> for Status {
43 fn from(err: GrpcError) -> Self {
44 match err {
45 GrpcError::IndexNotFound(name) => {
46 Status::not_found(format!("Index not found: {}", name))
47 }
48 GrpcError::IndexAlreadyExists(name) => {
49 Status::already_exists(format!("Index already exists: {}", name))
50 }
51 GrpcError::DimensionMismatch { expected, actual } => {
52 Status::invalid_argument(format!(
53 "Dimension mismatch: expected {}, got {}",
54 expected, actual
55 ))
56 }
57 GrpcError::InvalidRequest(msg) => Status::invalid_argument(msg),
58 GrpcError::HnswError(msg) => Status::internal(format!("HNSW error: {}", msg)),
59 GrpcError::Internal(msg) => Status::internal(msg),
60 }
61 }
62}