mpl_registry_api/
error.rs

1//! Registry API errors
2
3use axum::{
4    http::StatusCode,
5    response::{IntoResponse, Response},
6    Json,
7};
8use serde_json::json;
9use thiserror::Error;
10
11#[derive(Debug, Error)]
12pub enum RegistryError {
13    #[error("SType not found: {0}")]
14    NotFound(String),
15
16    #[error("Invalid SType format: {0}")]
17    InvalidFormat(String),
18
19    #[error("Schema parsing error: {0}")]
20    SchemaError(String),
21
22    #[error("IO error: {0}")]
23    IoError(#[from] std::io::Error),
24
25    #[error("Internal error: {0}")]
26    Internal(String),
27}
28
29impl IntoResponse for RegistryError {
30    fn into_response(self) -> Response {
31        let (status, error_code, message) = match &self {
32            RegistryError::NotFound(stype) => (
33                StatusCode::NOT_FOUND,
34                "E-NOT-FOUND",
35                format!("SType not found: {}", stype),
36            ),
37            RegistryError::InvalidFormat(msg) => (
38                StatusCode::BAD_REQUEST,
39                "E-INVALID-FORMAT",
40                msg.clone(),
41            ),
42            RegistryError::SchemaError(msg) => (
43                StatusCode::INTERNAL_SERVER_ERROR,
44                "E-SCHEMA-ERROR",
45                msg.clone(),
46            ),
47            RegistryError::IoError(e) => (
48                StatusCode::INTERNAL_SERVER_ERROR,
49                "E-IO-ERROR",
50                e.to_string(),
51            ),
52            RegistryError::Internal(msg) => (
53                StatusCode::INTERNAL_SERVER_ERROR,
54                "E-INTERNAL",
55                msg.clone(),
56            ),
57        };
58
59        let body = Json(json!({
60            "error": error_code,
61            "message": message,
62        }));
63
64        (status, body).into_response()
65    }
66}