Skip to main content

rust_webx_core/
error.rs

1//! Unified error type for the LRWF framework.
2
3use thiserror::Error;
4
5/// Framework-wide error type.
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("HTTP error: {0}")]
9    Http(String),
10
11    #[error("DI error: {0}")]
12    Di(String),
13
14    #[error("Routing error: {0}")]
15    Routing(String),
16
17    #[error("Serialization error: {0}")]
18    Serialization(#[from] serde_json::Error),
19
20    #[error("Internal error: {0}")]
21    Internal(String),
22
23    #[error("{0}")]
24    Message(String),
25
26    /// Validation error with user-readable message.
27    #[error("{0}")]
28    Validation(String),
29
30    /// Resource not found.
31    #[error("{0}")]
32    NotFound(String),
33
34    /// Optimistic concurrency or state conflict.
35    #[error("{0}")]
36    Conflict(String),
37}
38
39impl Error {
40    /// Map the error to an appropriate HTTP status code.
41    ///
42    /// Used by the built-in exception middleware to produce
43    /// well-formed HTTP error responses.
44    pub fn status_code(&self) -> u16 {
45        match self {
46            Error::Http(_) => 400,
47            Error::Di(_) => 500,
48            Error::Routing(_) => 404,
49            Error::Serialization(_) => 400,
50            Error::Internal(_) => 500,
51            Error::Message(_) => 500,
52            Error::Validation(_) => 400,
53            Error::NotFound(_) => 404,
54            Error::Conflict(_) => 409,
55        }
56    }
57}
58
59/// Shorthand for Result<T, Error>.
60pub type Result<T> = std::result::Result<T, Error>;