Skip to main content

alopex_server/
error.rs

1use axum::http::StatusCode;
2
3/// Server-wide result type.
4pub type Result<T> = std::result::Result<T, ServerError>;
5
6/// Top-level error type for alopex-server.
7#[derive(Debug, thiserror::Error)]
8pub enum ServerError {
9    #[error("invalid config: {0}")]
10    InvalidConfig(String),
11    #[error("bad request: {0}")]
12    BadRequest(String),
13    #[error("unauthorized: {0}")]
14    Unauthorized(String),
15    #[error("not found: {0}")]
16    NotFound(String),
17    #[error("conflict: {0}")]
18    Conflict(String),
19    #[error("payload too large: {0}")]
20    PayloadTooLarge(String),
21    #[error("timeout: {0}")]
22    Timeout(String),
23    #[error("session expired: {0}")]
24    SessionExpired(String),
25    #[error("sql error: {0}")]
26    Sql(#[from] alopex_sql::SqlError),
27    #[error("core error: {0}")]
28    Core(#[from] alopex_core::Error),
29    #[error("catalog error: {0}")]
30    Catalog(#[from] alopex_sql::catalog::CatalogError),
31    #[error("io error: {0}")]
32    Io(#[from] std::io::Error),
33    #[error("internal error: {0}")]
34    Internal(String),
35}
36
37impl ServerError {
38    /// Map error to HTTP status code.
39    pub fn status_code(&self) -> StatusCode {
40        match self {
41            Self::InvalidConfig(_) | Self::BadRequest(_) => StatusCode::BAD_REQUEST,
42            Self::Sql(err) if err.code() == "ALOPEX-S001" => StatusCode::CONFLICT,
43            Self::Sql(_) => StatusCode::BAD_REQUEST,
44            Self::Unauthorized(_) => StatusCode::UNAUTHORIZED,
45            Self::NotFound(_) => StatusCode::NOT_FOUND,
46            Self::Conflict(_) => StatusCode::CONFLICT,
47            Self::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
48            Self::Timeout(_) => StatusCode::REQUEST_TIMEOUT,
49            Self::SessionExpired(_) => StatusCode::GONE,
50            Self::Core(_) | Self::Catalog(_) | Self::Io(_) | Self::Internal(_) => {
51                StatusCode::INTERNAL_SERVER_ERROR
52            }
53        }
54    }
55
56    /// Map error to a stable error code for clients.
57    pub fn error_code(&self) -> String {
58        match self {
59            Self::Sql(err) => err.code().to_string(),
60            Self::InvalidConfig(_) => "INVALID_CONFIG".to_string(),
61            Self::BadRequest(_) => "INVALID_REQUEST".to_string(),
62            Self::Unauthorized(_) => "UNAUTHORIZED".to_string(),
63            Self::NotFound(_) => "NOT_FOUND".to_string(),
64            Self::Conflict(_) => "CONFLICT".to_string(),
65            Self::PayloadTooLarge(_) => "PAYLOAD_TOO_LARGE".to_string(),
66            Self::Timeout(_) => "QUERY_TIMEOUT".to_string(),
67            Self::SessionExpired(_) => "SESSION_EXPIRED".to_string(),
68            Self::Core(_) | Self::Catalog(_) | Self::Io(_) | Self::Internal(_) => {
69                "INTERNAL".to_string()
70            }
71        }
72    }
73}