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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use mio_extras::channel::TrySendError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Bad parameter: {reason}")]
BadParameter { reason: String },
#[error("Unsupported operation")]
Unsupported,
#[error("Out of resources")]
OutOfResources,
#[error("Entity not yet enabled")]
NotEnabled,
#[error("Attempted to modify immutable entity")]
ImmutablePolicy,
#[error("Inconsistent policies: {reason}")]
InconsistentPolicy { reason: String },
#[error("Precondition not met: {precondition}")]
PreconditionNotMet { precondition: String },
#[error("Illegal operation: {reason}")]
IllegalOperation { reason: String },
#[error("Lock poisoned")]
LockPoisoned,
#[error("Internal error: {reason}")]
Internal { reason: String },
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Serialization error: {reason}")]
Serialization { reason: String },
#[error("Discovery error: {reason}")]
Discovery { reason: String },
}
impl Error {
pub fn bad_parameter<T>(reason: impl Into<String>) -> Result<T> {
Err(Error::BadParameter {
reason: reason.into(),
})
}
pub fn precondition_not_met<T>(precondition: impl Into<String>) -> Result<T> {
Err(Error::PreconditionNotMet {
precondition: precondition.into(),
})
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_precondition_not_met {
($err_msg:literal) => {{
log::error!($err_msg);
Error::precondition_not_met($err_msg)
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_internal {
($($arg:tt)*) => (
{ log::error!($($arg)*);
Err( Error::Internal{ reason: format!($($arg)*) } )
}
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_discovery {
($($arg:tt)*) => (
{ error!($($arg)*);
Error::Message(format!($($arg)*) )
}
)
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(_e: std::sync::PoisonError<T>) -> Error {
Error::LockPoisoned
}
}
impl<T> From<TrySendError<T>> for Error
where
TrySendError<T>: std::error::Error,
{
fn from(e: TrySendError<T>) -> Error {
Error::Internal {
reason: format!("Cannot send to internal mio channel: {:?}", e),
}
}
}