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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Unified error type for the rust-webx framework.
use thiserror::Error;
/// Framework-wide error type.
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP error: {0}")]
Http(String),
#[error("Status {0}: {1}")]
Status(u16, String),
#[error("Unauthorized: {0}")]
Unauthorized(String),
#[error("Forbidden: {0}")]
Forbidden(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),
/// Optimistic concurrency or state conflict.
#[error("{0}")]
Conflict(String),
/// Request body (or one uploaded file) exceeded the configured limit.
#[error("{0}")]
PayloadTooLarge(String),
/// Request content type is not supported by the target endpoint.
#[error("{0}")]
UnsupportedMediaType(String),
/// A fully-formed problem response (validation, binding failure).
///
/// Carried as-is instead of being flattened into a message so the host can
/// write the status and the extension members (`errors`) verbatim.
#[error("request problem details")]
Problem(Box<crate::problem::ProblemDetails>),
}
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::Status(code, _) => *code,
Error::Unauthorized(_) => 401,
Error::Forbidden(_) => 403,
Error::Di(_) => 500,
Error::Routing(_) => 404,
Error::Serialization(_) => 400,
Error::Internal(_) => 500,
Error::Message(_) => 500,
Error::Validation(_) => 400,
Error::NotFound(_) => 404,
Error::Conflict(_) => 409,
Error::PayloadTooLarge(_) => 413,
Error::UnsupportedMediaType(_) => 415,
Error::Problem(problem) => problem.status,
}
}
}
/// Shorthand for Result<T, Error>.
pub type Result<T> = std::result::Result<T, Error>;