Skip to main content

sochdb_grpc/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// SochDB - LLM-Optimized Embedded Database
3// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Affero General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Affero General Public License for more details.
14//
15// You should have received a copy of the GNU Affero General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18//! Error types for gRPC service
19
20use thiserror::Error;
21use tonic::Status;
22
23/// Errors that can occur in the gRPC service
24#[derive(Error, Debug)]
25pub enum GrpcError {
26    #[error("Index not found: {0}")]
27    IndexNotFound(String),
28    
29    #[error("Index already exists: {0}")]
30    IndexAlreadyExists(String),
31    
32    #[error("Invalid dimension: expected {expected}, got {actual}")]
33    DimensionMismatch { expected: usize, actual: usize },
34    
35    #[error("Invalid request: {0}")]
36    InvalidRequest(String),
37    
38    #[error("HNSW error: {0}")]
39    HnswError(String),
40    
41    #[error("Internal error: {0}")]
42    Internal(String),
43}
44
45impl From<GrpcError> for Status {
46    fn from(err: GrpcError) -> Self {
47        match err {
48            GrpcError::IndexNotFound(name) => {
49                Status::not_found(format!("Index not found: {}", name))
50            }
51            GrpcError::IndexAlreadyExists(name) => {
52                Status::already_exists(format!("Index already exists: {}", name))
53            }
54            GrpcError::DimensionMismatch { expected, actual } => {
55                Status::invalid_argument(format!(
56                    "Dimension mismatch: expected {}, got {}",
57                    expected, actual
58                ))
59            }
60            GrpcError::InvalidRequest(msg) => Status::invalid_argument(msg),
61            GrpcError::HnswError(msg) => Status::internal(format!("HNSW error: {}", msg)),
62            GrpcError::Internal(msg) => Status::internal(msg),
63        }
64    }
65}