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

Commit

Permalink
Extend ResultExt to Option, allow chaining with boxed Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrnbrg committed Jun 27, 2017
1 parent 62627ed commit 95e5219
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "error-chain"
version = "0.10.1-pre"
version = "0.10.0"
authors = [ "Brian Anderson <banderson@mozilla.com>",
"Paul Colomiets <paul@colomiets.name>",
"Colin Kiegel <kiegel@gmx.de>",
Expand Down
25 changes: 23 additions & 2 deletions src/error_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ macro_rules! error_chain_processed {
)
}

/// Construct a chained error from another boxed error and a kind, and generates a backtrace
pub fn with_boxed_chain<K>(error: Box<::std::error::Error + Send>, kind: K)
-> $error_name
where K: Into<$error_kind_name>
{
$error_name(
kind.into(),
$crate::State::new::<$error_name>(error, ),
)
}

/// Returns the kind of the error.
pub fn kind(&self) -> &$error_kind_name {
&self.0
Expand Down Expand Up @@ -302,7 +313,7 @@ macro_rules! error_chain_processed {
// The ResultExt trait defines the `chain_err` method.

/// Additional methods for `Result`, for easy interaction with this crate.
pub trait $result_ext_name<T, E> {
pub trait $result_ext_name<T> {
/// If the `Result` is an `Err` then `chain_err` evaluates the closure,
/// which returns *some type that can be converted to `ErrorKind`*, boxes
/// the original error to store as the cause, then returns a new error
Expand All @@ -312,7 +323,7 @@ macro_rules! error_chain_processed {
EK: Into<$error_kind_name>;
}

impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
where F: FnOnce() -> EK,
EK: Into<$error_kind_name> {
Expand All @@ -323,6 +334,16 @@ macro_rules! error_chain_processed {
}
}

impl<T> $result_ext_name<T> for ::std::option::Option<T> {
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
where F: FnOnce() -> EK,
EK: Into<$error_kind_name> {
self.ok_or_else(move || {
$crate::ChainedError::from_kind(callback().into())
})
}
}


};
}
Expand Down

0 comments on commit 95e5219

Please sign in to comment.