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
use std::error::Error as ErrorTrait;
use std::fmt::Display;
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub context: Option<String>,
}
impl Error {
pub(crate) fn new(kind: ErrorKind) -> Error {
Error {
kind,
context: None,
}
}
pub(crate) fn new_with_context(kind: ErrorKind, context: impl Display) -> Error {
Error {
kind,
context: Some(context.to_string()),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} (context: {})",
self.kind.description(),
self.context.as_ref().unwrap_or(&"none".to_string())
)
}
}
impl ErrorTrait for Error {}
#[derive(Debug)]
pub enum ErrorKind {
ServerFailure,
ServerBusy,
Deadlock,
ResponseFailure,
InvalidStatusCode,
RequestBodyFailure,
ResponseBodyFailure,
FileNotFound,
}
impl ErrorKind {
fn description(&self) -> &'static str {
match self {
ErrorKind::ServerFailure => "the server is not running",
ErrorKind::ServerBusy => "the server is busy",
ErrorKind::Deadlock => "a lock can't be bypassed",
ErrorKind::ResponseFailure => "could not deliver a response",
ErrorKind::InvalidStatusCode => "invalid status code",
ErrorKind::RequestBodyFailure => "failed to read the request body",
ErrorKind::ResponseBodyFailure => "failed to write the response body",
ErrorKind::FileNotFound => "file not found",
}
}
}