use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Collection not found: {0}")]
CollectionNotFound(String),
#[error("Collection already exists: {0}")]
CollectionExists(String),
#[error("Point not found: {0}")]
PointNotFound(String),
#[error("Invalid request: {0}")]
InvalidRequest(String),
#[error("Core error: {0}")]
Core(#[from] ruvector_core::RuvectorError),
#[error("Server error: {0}")]
Server(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Internal error: {0}")]
Internal(String),
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
let (status, error_message) = match self {
Error::CollectionNotFound(_) | Error::PointNotFound(_) => {
(StatusCode::NOT_FOUND, self.to_string())
}
Error::CollectionExists(_) => (StatusCode::CONFLICT, self.to_string()),
Error::InvalidRequest(_) => (StatusCode::BAD_REQUEST, self.to_string()),
Error::Core(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
Error::Server(_) | Error::Internal(_) => {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
}
Error::Config(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
Error::Serialization(e) => (StatusCode::BAD_REQUEST, e.to_string()),
};
let body = Json(json!({
"error": error_message,
"status": status.as_u16(),
}));
(status, body).into_response()
}
}