Skip to main content

lrwf_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
35impl Error {
36    /// Map the error to an appropriate HTTP status code.
37    ///
38    /// Used by the built-in exception middleware to produce
39    /// well-formed HTTP error responses.
40    pub fn status_code(&self) -> u16 {
41        match self {
42            Error::Http(_) => 400,
43            Error::Di(_) => 500,
44            Error::Routing(_) => 404,
45            Error::Serialization(_) => 400,
46            Error::Internal(_) => 500,
47            Error::Message(_) => 500,
48            Error::Validation(_) => 400,
49            Error::NotFound(_) => 404,
50        }
51    }
52}
53
54/// Shorthand for Result<T, Error>.
55pub type Result<T> = std::result::Result<T, Error>;