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