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

Commit e5a0a1a

Browse files
committed
Clarify cause iteration behavior
1 parent ba173b5 commit e5a0a1a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

book/src/fail.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ while let Some(cause) = fail.cause() {
5959
}
6060
```
6161

62+
For convenience an iterator is also provided:
63+
64+
```rust
65+
// Assume err is a type that implements `Fail`
66+
let mut fail: &Fail = err;
67+
68+
for cause in fail.iter_causes() {
69+
println!("{}", cause);
70+
}
71+
```
72+
6273
## Backtraces
6374

6475
Errors can also generate a backtrace when they are constructed, helping you

src/error/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Error {
133133
}
134134

135135
/// Returns a iterator over the causes of the `Error`, beginning with
136-
/// the failure returned by the `cause` method and ending with the failure
136+
/// the failure returned by the `as_fail` method and ending with the failure
137137
/// returned by `find_root_cause`.
138138
pub fn iter_causes(&self) -> Causes {
139139
self.as_fail().iter_causes()

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ impl Fail {
222222

223223
/// Returns a iterator over the causes of this `Fail` with itself
224224
/// as the first item and the `root_cause` as the final item.
225+
///
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)`).
225229
pub fn iter_causes(&self) -> Causes {
226230
Causes { fail: Some(self) }
227231
}

0 commit comments

Comments
 (0)