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

Clarify iteration behavior for causes by splitting it in two methods #232

Merged
merged 2 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,24 @@ impl Error {
self.as_fail().find_root_cause()
}

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

/// Returns a iterator over all fails up the chain from the current
/// as the first item up to the `root_cause` as the final item.
///
/// This means that the chain also includes the fail itself which
/// means that it does *not* start with `cause`. To skip the outermost
/// fail use `iter_causes` instead.
pub fn iter_chain(&self) -> Causes {
self.as_fail().iter_chain()
}

/// Attempts to downcast this `Error` to a particular `Fail` type by
/// reference.
///
Expand All @@ -162,7 +173,7 @@ impl Error {
}

/// Deprecated alias to `iter_causes`.
#[deprecated(since = "0.1.2", note = "please use the 'iter_causes()' method instead")]
#[deprecated(since = "0.1.2", note = "please use the 'iter_chain()' method instead")]
pub fn causes(&self) -> Causes {
Causes { fail: Some(self.as_fail()) }
}
Expand Down
20 changes: 14 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,21 @@ impl Fail {
find_root_cause(self)
}

/// Returns a iterator over the causes of this `Fail` with itself
/// as the first item and the `root_cause` as the final item.
/// Returns a iterator over the causes of this `Fail` with the cause
/// of this fail as the first item and the `root_cause` as the final item.
///
/// This means that `causes` also includes the fail itself which
/// means that it does *not* start with `cause`. To skip the outermost
/// fail use the `skip` method (`fail.causes().skip(1)`).
/// Use `iter_chain` to also include the fail itself.
pub fn iter_causes(&self) -> Causes {
Causes { fail: self.cause() }
}

/// Returns a iterator over all fails up the chain from the current
/// as the first item up to the `root_cause` as the final item.
///
/// This means that the chain also includes the fail itself which
/// means that it does *not* start with `cause`. To skip the outermost
/// fail use `iter_causes` instead.
pub fn iter_chain(&self) -> Causes {
Causes { fail: Some(self) }
}

Expand All @@ -237,7 +245,7 @@ impl Fail {
}

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