Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 94158d2

Browse files
authored
Clarify iteration behavior for causes by splitting it in two methods (#232)
1 parent 3d5b8f9 commit 94158d2

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/error/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,24 @@ impl Error {
132132
self.as_fail().find_root_cause()
133133
}
134134

135-
/// Returns a iterator over the causes of the `Error`, beginning with
136-
/// the failure returned by the `as_fail` method and ending with the failure
137-
/// returned by `find_root_cause`.
135+
/// Returns a iterator over the causes of this error with the cause
136+
/// of the fail as the first item and the `root_cause` as the final item.
137+
///
138+
/// Use `iter_chain` to also include the fail of this error itself.
138139
pub fn iter_causes(&self) -> Causes {
139140
self.as_fail().iter_causes()
140141
}
141142

143+
/// Returns a iterator over all fails up the chain from the current
144+
/// as the first item up to the `root_cause` as the final item.
145+
///
146+
/// This means that the chain also includes the fail itself which
147+
/// means that it does *not* start with `cause`. To skip the outermost
148+
/// fail use `iter_causes` instead.
149+
pub fn iter_chain(&self) -> Causes {
150+
self.as_fail().iter_chain()
151+
}
152+
142153
/// Attempts to downcast this `Error` to a particular `Fail` type by
143154
/// reference.
144155
///
@@ -162,7 +173,7 @@ impl Error {
162173
}
163174

164175
/// Deprecated alias to `iter_causes`.
165-
#[deprecated(since = "0.1.2", note = "please use the 'iter_causes()' method instead")]
176+
#[deprecated(since = "0.1.2", note = "please use the 'iter_chain()' method instead")]
166177
pub fn causes(&self) -> Causes {
167178
Causes { fail: Some(self.as_fail()) }
168179
}

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,21 @@ impl Fail {
220220
find_root_cause(self)
221221
}
222222

223-
/// Returns a iterator over the causes of this `Fail` with itself
224-
/// as the first item and the `root_cause` as the final item.
223+
/// Returns a iterator over the causes of this `Fail` with the cause
224+
/// of this fail as the first item and the `root_cause` as the final item.
225225
///
226-
/// This means that `causes` also includes the fail itself which
227-
/// means that it does *not* start with `cause`. To skip the outermost
228-
/// fail use the `skip` method (`fail.causes().skip(1)`).
226+
/// Use `iter_chain` to also include the fail itself.
229227
pub fn iter_causes(&self) -> Causes {
228+
Causes { fail: self.cause() }
229+
}
230+
231+
/// Returns a iterator over all fails up the chain from the current
232+
/// as the first item up to the `root_cause` as the final item.
233+
///
234+
/// This means that the chain also includes the fail itself which
235+
/// means that it does *not* start with `cause`. To skip the outermost
236+
/// fail use `iter_causes` instead.
237+
pub fn iter_chain(&self) -> Causes {
230238
Causes { fail: Some(self) }
231239
}
232240

@@ -237,7 +245,7 @@ impl Fail {
237245
}
238246

239247
/// Deprecated alias to `iter_causes`.
240-
#[deprecated(since = "0.1.2", note = "please use the 'iter_causes()' method instead")]
248+
#[deprecated(since = "0.1.2", note = "please use the 'iter_chain()' method instead")]
241249
pub fn causes(&self) -> Causes {
242250
Causes { fail: Some(self) }
243251
}

0 commit comments

Comments
 (0)