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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use miette::Diagnostic;
use thiserror::Error;
use crate::ErrTypeTraits;
#[derive(Error, Debug, Diagnostic)]
pub enum GracefulShutdownError<ErrType: ErrTypeTraits = crate::BoxedError> {
#[error("at least one subsystem returned an error")]
SubsystemsFailed(#[related] Vec<SubsystemError<ErrType>>),
#[error("shutdown timed out")]
ShutdownTimeout(#[related] Vec<SubsystemError<ErrType>>),
}
impl<ErrType: ErrTypeTraits> GracefulShutdownError<ErrType> {
pub fn into_subsystem_errors(self) -> Vec<SubsystemError<ErrType>> {
match self {
GracefulShutdownError::SubsystemsFailed(rel) => rel,
GracefulShutdownError::ShutdownTimeout(rel) => rel,
}
}
pub fn get_subsystem_errors(&self) -> &Vec<SubsystemError<ErrType>> {
match self {
GracefulShutdownError::SubsystemsFailed(rel) => rel,
GracefulShutdownError::ShutdownTimeout(rel) => rel,
}
}
}
#[derive(Debug, Error, Diagnostic)]
pub enum PartialShutdownError<ErrType: ErrTypeTraits = crate::BoxedError> {
#[error("at least one subsystem returned an error")]
SubsystemsFailed(#[related] Vec<SubsystemError<ErrType>>),
#[error("unable to find nested subsystem in given subsystem")]
SubsystemNotFound,
#[error("unable to perform partial shutdown, the program is already shutting down")]
AlreadyShuttingDown,
}
pub struct SubsystemFailure<ErrType>(pub(crate) ErrType);
impl<ErrType> std::ops::Deref for SubsystemFailure<ErrType> {
type Target = ErrType;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<ErrType> SubsystemFailure<ErrType> {
pub fn get_error(&self) -> &ErrType {
&self.0
}
pub fn into_error(self) -> ErrType {
self.0
}
}
impl<ErrType> std::fmt::Debug for SubsystemFailure<ErrType>
where
ErrType: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
impl<ErrType> std::fmt::Display for SubsystemFailure<ErrType>
where
ErrType: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl<ErrType> std::error::Error for SubsystemFailure<ErrType> where
ErrType: std::fmt::Display + std::fmt::Debug
{
}
#[derive(Debug, Error, Diagnostic)]
pub enum SubsystemError<ErrType: ErrTypeTraits = crate::BoxedError> {
#[error("Error in subsystem '{0}'")]
Failed(String, #[source] SubsystemFailure<ErrType>),
#[error("Subsystem '{0}' was aborted")]
Cancelled(String),
#[error("Subsystem '{0}' panicked")]
Panicked(String),
}
impl<ErrType: ErrTypeTraits> SubsystemError<ErrType> {
pub fn name(&self) -> &str {
match self {
SubsystemError::Failed(name, _) => name,
SubsystemError::Cancelled(name) => name,
SubsystemError::Panicked(name) => name,
}
}
}
#[derive(Error, Debug, Diagnostic)]
#[error("A shutdown request caused this task to be cancelled")]
pub struct CancelledByShutdown;
#[cfg(test)]
mod tests {
use crate::BoxedError;
use super::*;
fn examine_report(report: miette::Report) {
println!("{}", report);
println!("{:?}", report);
let boxed_error: BoxedError = report.into();
println!("{}", boxed_error);
println!("{:?}", boxed_error);
}
#[test]
fn errors_can_be_converted_to_diagnostic() {
examine_report(GracefulShutdownError::ShutdownTimeout::<BoxedError>(vec![]).into());
examine_report(GracefulShutdownError::SubsystemsFailed::<BoxedError>(vec![]).into());
examine_report(PartialShutdownError::AlreadyShuttingDown::<BoxedError>.into());
examine_report(PartialShutdownError::SubsystemNotFound::<BoxedError>.into());
examine_report(PartialShutdownError::SubsystemsFailed::<BoxedError>(vec![]).into());
examine_report(SubsystemError::Cancelled::<BoxedError>("".into()).into());
examine_report(SubsystemError::Panicked::<BoxedError>("".into()).into());
examine_report(
SubsystemError::Failed::<BoxedError>("".into(), SubsystemFailure("".into())).into(),
);
examine_report(CancelledByShutdown.into());
}
#[test]
fn extract_related_from_graceful_shutdown_error() {
let related = || {
vec![
SubsystemError::Cancelled("a".into()),
SubsystemError::Panicked("b".into()),
]
};
let matches_related = |data: &Vec<SubsystemError<BoxedError>>| {
let mut iter = data.into_iter();
let elem = iter.next().unwrap();
assert_eq!(elem.name(), "a");
assert!(matches!(elem, SubsystemError::Cancelled(_)));
let elem = iter.next().unwrap();
assert_eq!(elem.name(), "b");
assert!(matches!(elem, SubsystemError::Panicked(_)));
assert!(iter.next().is_none());
};
matches_related(GracefulShutdownError::ShutdownTimeout(related()).get_subsystem_errors());
matches_related(GracefulShutdownError::SubsystemsFailed(related()).get_subsystem_errors());
matches_related(&GracefulShutdownError::ShutdownTimeout(related()).into_subsystem_errors());
matches_related(
&GracefulShutdownError::SubsystemsFailed(related()).into_subsystem_errors(),
);
}
#[test]
fn extract_contained_error_from_convert_subsystem_failure() {
let msg = "MyFailure".to_string();
let failure = SubsystemFailure(msg.clone());
assert_eq!(&msg, failure.get_error());
assert_eq!(msg, *failure);
assert_eq!(msg, failure.into_error());
}
}