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
91
92
93
use crate::error::{ErrCode, Error};
use std::fmt;
macro_rules! dep_error {
($error:ty, $kind:expr, $code:expr, $reason:expr) => {
impl From<$error> for Error {
#[must_use]
fn from(inner: $error) -> Self {
Self::new($code, $reason, Some($kind(inner)))
}
}
};
}
dep_error!(
std::env::VarError,
ErrSource::Var,
ErrCode::Env,
"There was an error processing your enviroment"
);
dep_error!(
std::io::Error,
ErrSource::Io,
ErrCode::Io,
"There was an error processing your request"
);
dep_error!(
std::num::TryFromIntError,
ErrSource::TryFromInt,
ErrCode::Parse,
"There was an error trying to convert an integer"
);
dep_error!(
std::num::ParseIntError,
ErrSource::ParseInt,
ErrCode::Parse,
"There was an error trying to convert to an integer"
);
dep_error!(
std::array::TryFromSliceError,
ErrSource::TryFromSlice,
ErrCode::Protocol,
"There was an error converting bytes to isize"
);
dep_error!(
std::path::StripPrefixError,
ErrSource::StripPrefix,
ErrCode::Parse,
"There was an error trying to strip a prefix from a path"
);
#[derive(Debug)]
#[allow(clippy::large_enum_variant, variant_size_differences)]
crate enum ErrSource {
Io(std::io::Error),
ParseInt(std::num::ParseIntError),
StripPrefix(std::path::StripPrefixError),
TryFromInt(std::num::TryFromIntError),
TryFromSlice(std::array::TryFromSliceError),
Var(std::env::VarError),
}
impl std::error::Error for ErrSource {}
impl fmt::Display for ErrSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(source) => write!(f, "{}", source),
Self::ParseInt(source) => write!(f, "{}", source),
Self::StripPrefix(source) => write!(f, "{}", source),
Self::TryFromInt(source) => write!(f, "{}", source),
Self::TryFromSlice(source) => write!(f, "{}", source),
Self::Var(source) => write!(f, "{}", source),
}
}
}