toondb_grpc/
error.rs

1// Copyright 2025 Sushanth (https://github.com/sushanthpy)
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Error types for gRPC service
16
17use thiserror::Error;
18use tonic::Status;
19
20/// Errors that can occur in the gRPC service
21#[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}