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("future distributed execution required: {0}")]
26    FutureDistributedExecutionRequired(String),
27    #[error("restore integrity mismatch: {0}")]
28    RestoreIntegrityMismatch(String),
29    #[error("sql error: {0}")]
30    Sql(#[from] alopex_sql::SqlError),
31    #[error("core error: {0}")]
32    Core(#[from] alopex_core::Error),
33    #[error("catalog error: {0}")]
34    Catalog(#[from] alopex_sql::catalog::CatalogError),
35    #[error("io error: {0}")]
36    Io(#[from] std::io::Error),
37    #[error("internal error: {0}")]
38    Internal(String),
39}
40
41impl ServerError {
42    /// Map error to HTTP status code.
43    pub fn status_code(&self) -> StatusCode {
44        match self {
45            Self::InvalidConfig(_) | Self::BadRequest(_) => StatusCode::BAD_REQUEST,
46            Self::Sql(err) if err.code() == "ALOPEX-S001" => StatusCode::CONFLICT,
47            Self::Sql(_) => StatusCode::BAD_REQUEST,
48            Self::Unauthorized(_) => StatusCode::UNAUTHORIZED,
49            Self::NotFound(_) => StatusCode::NOT_FOUND,
50            Self::Conflict(_) => StatusCode::CONFLICT,
51            Self::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
52            Self::Timeout(_) => StatusCode::REQUEST_TIMEOUT,
53            Self::SessionExpired(_) => StatusCode::GONE,
54            Self::FutureDistributedExecutionRequired(_) => StatusCode::NOT_IMPLEMENTED,
55            Self::RestoreIntegrityMismatch(_) => StatusCode::CONFLICT,
56            Self::Core(_) | Self::Catalog(_) | Self::Io(_) | Self::Internal(_) => {
57                StatusCode::INTERNAL_SERVER_ERROR
58            }
59        }
60    }
61
62    /// Map error to a stable error code for clients.
63    pub fn error_code(&self) -> String {
64        match self {
65            Self::Sql(err) => err.code().to_string(),
66            Self::InvalidConfig(_) => "INVALID_CONFIG".to_string(),
67            Self::BadRequest(_) => "INVALID_REQUEST".to_string(),
68            Self::Unauthorized(_) => "UNAUTHORIZED".to_string(),
69            Self::NotFound(_) => "NOT_FOUND".to_string(),
70            Self::Conflict(_) => "CONFLICT".to_string(),
71            Self::PayloadTooLarge(_) => "PAYLOAD_TOO_LARGE".to_string(),
72            Self::Timeout(_) => "QUERY_TIMEOUT".to_string(),
73            Self::SessionExpired(_) => "SESSION_EXPIRED".to_string(),
74            Self::FutureDistributedExecutionRequired(_) => {
75                "FUTURE_DISTRIBUTED_EXECUTION_REQUIRED".to_string()
76            }
77            Self::RestoreIntegrityMismatch(_) => "RESTORE_INTEGRITY_MISMATCH".to_string(),
78            Self::Core(_) | Self::Catalog(_) | Self::Io(_) | Self::Internal(_) => {
79                "INTERNAL".to_string()
80            }
81        }
82    }
83}