1use axum::{
4 http::StatusCode,
5 response::{IntoResponse, Response},
6 Json,
7};
8use serde_json::json;
9
10pub type Result<T> = std::result::Result<T, Error>;
12
13#[derive(Debug, thiserror::Error)]
15pub enum Error {
16 #[error("Collection not found: {0}")]
18 CollectionNotFound(String),
19
20 #[error("Collection already exists: {0}")]
22 CollectionExists(String),
23
24 #[error("Point not found: {0}")]
26 PointNotFound(String),
27
28 #[error("Invalid request: {0}")]
30 InvalidRequest(String),
31
32 #[error("Core error: {0}")]
34 Core(#[from] ruvector_core::RuvectorError),
35
36 #[error("Server error: {0}")]
38 Server(String),
39
40 #[error("Configuration error: {0}")]
42 Config(String),
43
44 #[error("Serialization error: {0}")]
46 Serialization(#[from] serde_json::Error),
47
48 #[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}