Skip to main content

axum_apcore/
errors.rs

1// Error types for axum-apcore.
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5
6/// Unified error type for axum-apcore operations.
7#[derive(Debug, thiserror::Error)]
8pub enum AxumApcoreError {
9    #[error("Configuration error: {0}")]
10    Config(String),
11
12    #[error("Scanner error: {0}")]
13    Scanner(String),
14
15    #[error("Registration error: {0}")]
16    Registration(String),
17
18    #[error("Context extraction error: {0}")]
19    Context(String),
20
21    #[error("Module execution error: {0}")]
22    Execution(#[from] apcore::ModuleError),
23
24    #[error("Regex error: {0}")]
25    Regex(#[from] regex::Error),
26
27    #[error("JSON error: {0}")]
28    Json(#[from] serde_json::Error),
29}
30
31impl IntoResponse for AxumApcoreError {
32    fn into_response(self) -> Response {
33        let (status, message) = match &self {
34            AxumApcoreError::Config(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
35            AxumApcoreError::Scanner(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
36            AxumApcoreError::Registration(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
37            AxumApcoreError::Context(msg) => (StatusCode::UNAUTHORIZED, msg.clone()),
38            AxumApcoreError::Execution(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.message.clone()),
39            AxumApcoreError::Regex(e) => (StatusCode::BAD_REQUEST, e.to_string()),
40            AxumApcoreError::Json(e) => (StatusCode::BAD_REQUEST, e.to_string()),
41        };
42
43        let body = serde_json::json!({
44            "error": message,
45        });
46        (status, axum::Json(body)).into_response()
47    }
48}