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
//! HTTP handler and kernel errors.
/// Failure while handling a request.
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
pub enum HttpError {
/// Handler failed without a specific status (maps to HTTP 500).
#[error("request handler failed: {0}")]
Failed(String),
/// Handler failed with an HTTP status to map onto the response.
#[error("HTTP {status}: {message}")]
Status {
/// Suggested status code.
status: u16,
/// Error text.
message: String,
},
}
impl HttpError {
/// Handler failure that maps to HTTP 500 by default.
#[must_use]
pub fn failed(message: impl Into<String>) -> Self {
Self::Failed(message.into())
}
/// Failure that maps to `status`.
#[must_use]
pub fn status(status: u16, message: impl Into<String>) -> Self {
Self::Status {
status,
message: message.into(),
}
}
/// Convenience: HTTP 404.
#[must_use]
pub fn not_found(message: impl Into<String>) -> Self {
Self::status(404, message)
}
/// Convenience: HTTP 400.
#[must_use]
pub fn bad_request(message: impl Into<String>) -> Self {
Self::status(400, message)
}
/// Convenience: HTTP 422.
#[must_use]
pub fn unprocessable(message: impl Into<String>) -> Self {
Self::status(422, message)
}
/// Status used by [`crate::DefaultExceptionHandler`].
#[must_use]
pub const fn status_code(&self) -> u16 {
match self {
Self::Failed(_) => 500,
Self::Status { status, .. } => *status,
}
}
/// Human-readable message (without the `HTTP {status}:` prefix).
#[must_use]
pub fn message(&self) -> &str {
match self {
Self::Failed(message) | Self::Status { message, .. } => message,
}
}
}