ruvector_server/
error.rs

1//! Error types for the ruvector server
2
3use axum::{
4    http::StatusCode,
5    response::{IntoResponse, Response},
6    Json,
7};
8use serde_json::json;
9
10/// Result type for server operations
11pub type Result<T> = std::result::Result<T, Error>;
12
13/// Server error types
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    /// Collection not found
17    #[error("Collection not found: {0}")]
18    CollectionNotFound(String),
19
20    /// Collection already exists
21    #[error("Collection already exists: {0}")]
22    CollectionExists(String),
23
24    /// Point not found
25    #[error("Point not found: {0}")]
26    PointNotFound(String),
27
28    /// Invalid request
29    #[error("Invalid request: {0}")]
30    InvalidRequest(String),
31
32    /// Core library error
33    #[error("Core error: {0}")]
34    Core(#[from] ruvector_core::RuvectorError),
35
36    /// Server error
37    #[error("Server error: {0}")]
38    Server(String),
39
40    /// Configuration error
41    #[error("Configuration error: {0}")]
42    Config(String),
43
44    /// Serialization error
45    #[error("Serialization error: {0}")]
46    Serialization(#[from] serde_json::Error),
47
48    /// Internal error
49    #[error("Internal error: {0}")]
50    Internal(String),
51}
52
53impl IntoResponse for Error {
54    fn into_response(self) -> Response {
55        let (status, error_message) = match self {
56            Error::CollectionNotFound(_) | Error::PointNotFound(_) => {
57                (StatusCode::NOT_FOUND, self.to_string())
58            }
59            Error::CollectionExists(_) => (StatusCode::CONFLICT, self.to_string()),
60            Error::InvalidRequest(_) => (StatusCode::BAD_REQUEST, self.to_string()),
61            Error::Core(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
62            Error::Server(_) | Error::Internal(_) => {
63                (StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
64            }
65            Error::Config(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
66            Error::Serialization(e) => (StatusCode::BAD_REQUEST, e.to_string()),
67        };
68
69        let body = Json(json!({
70            "error": error_message,
71            "status": status.as_u16(),
72        }));
73
74        (status, body).into_response()
75    }
76}