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
use crate::Attachments;
use super::{
super::{error::*, problem::*},
cause::*,
r#ref::*,
};
use std::error::*;
//
// Causes
//
/// Access to causes.
pub trait Causes {
/// The problem that owns this causation chain.
fn problem(&self) -> &Problem;
/// Iterate causes in order of causation.
///
/// You can call [iter_sources](IterateErrorSources::iter_sources) on each to further
/// iterate the "branches" of the tree:
///
/// ```
/// for cause in problem.iter_causes() {
/// for error in error.iter_sources() {
/// println!("{}", error);
/// }
/// }
/// ```
fn iter_causes(&self) -> impl Iterator<Item = &Cause>;
/// Iterate causes with an error of a type.
///
/// Will recurse into [source](Error::source).
fn iter_causes_with_error_type<ErrorT>(&self) -> impl Iterator<Item = CauseRef<'_, ErrorT>>
where
ErrorT: 'static + Error,
{
self.iter_causes().enumerate().filter_map(|(depth, cause)| {
cause
.iter_errors_of_type()
.next()
.map(|error| CauseRef::new(self.problem(), depth, error, &cause.attachments))
})
}
/// Iterate causes in order of causation until (but not including) a cause with an error of a
/// type.
///
/// Will recurse into [source](Error::source).
fn iter_causes_until_error_type<ErrorT>(&self) -> impl Iterator<Item = &Cause>
where
ErrorT: 'static + Error,
{
self.iter_causes()
.take_while(|cause| !cause.has_error_of_type::<ErrorT>())
}
/// Iterate causes in order of causation from a cause with an error of a type.
///
/// Will recurse into [source](Error::source).
fn iter_causes_from_error_type<ErrorT>(&self) -> impl Iterator<Item = &Cause>
where
ErrorT: 'static + Error,
{
self.iter_causes()
.skip_while(|cause| !cause.has_error_of_type::<ErrorT>())
}
/// Iterate causes with the error.
///
/// Will recurse into [source](Error::source).
fn iter_causes_with_error<ErrorT>(
&self,
error: &ErrorT,
) -> impl Iterator<Item = CauseRef<'_, ErrorT>>
where
ErrorT: 'static + Error + PartialEq,
{
self.iter_causes().enumerate().filter_map(|(depth, cause)| {
cause
.error_for(error)
.map(|error| CauseRef::new(self.problem(), depth, error, &cause.attachments))
})
}
/// Iterate causes with an attachment of a type.
fn iter_causes_with_attachment_type<AttachmentT>(
&self,
) -> impl Iterator<Item = (&Cause, &AttachmentT)>
where
AttachmentT: 'static,
{
self.iter_causes()
.filter_map(|cause| match cause.attachment_of_type() {
Some(attachment) => Some((cause, attachment)),
None => None,
})
}
/// The first cause with an error of a type.
///
/// Will recurse into [source](Error::source).
fn cause_with_error_type<ErrorT>(&self) -> Option<CauseRef<'_, ErrorT>>
where
ErrorT: 'static + Error,
{
self.iter_causes_with_error_type().next()
}
/// The first cause with the error.
///
/// Will recurse into [source](Error::source).
fn cause_with_error<ErrorT>(&self, error: &ErrorT) -> Option<CauseRef<'_, ErrorT>>
where
ErrorT: 'static + Error + PartialEq,
{
self.iter_causes_with_error(error).next()
}
/// The first cause with an attachment of a type.
fn cause_with_attachment_type<AttachmentT>(&self) -> Option<(&Cause, &AttachmentT)>
where
AttachmentT: 'static,
{
self.iter_causes_with_attachment_type().next()
}
/// Iterate errors in order of causation.
///
/// Note that this will *not* include [source](Error::source), however you can call
/// [iter_sources](IterateErrorSources::iter_sources) on each to further iterate the
/// "branches" of the tree:
///
/// ```
/// for error in problem.iter_errors() {
/// for error in error.as_ref().iter_sources() {
/// println!("{}", error);
/// }
/// }
/// ```
fn iter_errors(&self) -> impl Iterator<Item = &CapturedError> {
self.iter_causes().map(|cause| &cause.error)
}
/// Iterate errors of a type in order of causation.
///
/// Note that this will *not* include [source](Error::source).
fn iter_errors_of_type<ErrorT>(&self) -> impl Iterator<Item = &ErrorT>
where
ErrorT: 'static + Error,
{
self.iter_errors()
.filter_map(|error| error.downcast_ref::<ErrorT>())
}
/// The first error of a type.
///
/// Note that this will *not* include [source](Error::source).
fn error_of_type<ErrorT>(&self) -> Option<&ErrorT>
where
ErrorT: 'static + Error,
{
self.iter_errors_of_type().next()
}
/// Whether we have an error of a type in the causation chain.
///
/// Will recurse into [source](Error::source).
fn has_error_type<ErrorT>(&self) -> bool
where
ErrorT: 'static + Error,
{
self.cause_with_error_type::<ErrorT>().is_some()
}
/// Whether we have the error in the causation chain.
///
/// Will recurse into [source](Error::source).
fn has_error<ErrorT>(&self, error: &ErrorT) -> bool
where
ErrorT: 'static + Error + PartialEq,
{
self.cause_with_error(error).is_some()
}
}