Skip to main content

adminx_core/
error.rs

1// adminx-core/src/error.rs
2use std::fmt;
3
4/// Framework-neutral error. Adapters map this onto their own response type.
5#[derive(Debug, Clone)]
6pub enum CoreError {
7    NotFound,
8    BadRequest(String),
9    Unauthorized,
10    Forbidden,
11    Internal(String),
12}
13
14impl CoreError {
15    /// HTTP status code this error maps to.
16    pub fn status(&self) -> u16 {
17        match self {
18            CoreError::NotFound => 404,
19            CoreError::BadRequest(_) => 400,
20            CoreError::Unauthorized => 401,
21            CoreError::Forbidden => 403,
22            CoreError::Internal(_) => 500,
23        }
24    }
25
26    pub fn message(&self) -> String {
27        match self {
28            CoreError::NotFound => "Not Found".to_string(),
29            CoreError::BadRequest(m) => format!("Bad Request: {m}"),
30            CoreError::Unauthorized => "Unauthorized".to_string(),
31            CoreError::Forbidden => "Forbidden".to_string(),
32            CoreError::Internal(m) => format!("Internal Server Error: {m}"),
33        }
34    }
35}
36
37impl fmt::Display for CoreError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(&self.message())
40    }
41}
42
43impl std::error::Error for CoreError {}