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

Commit e2bdf55

Browse files
dgrnbrgYamakaky
authored andcommitted
Extend ResultExt to Option, allow chaining with boxed Errors
1 parent 3d0df86 commit e2bdf55

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Unreleased
22

33
- [Add a new method for `Error`: `chain_err`.](https://github.com/brson/error-chain/pull/141)
4+
- [Allow `chain_err` to be used on `Option<T>`](https://github.com/brson/error-chain/pull/156)
5+
- [Add support for creating an error chain on boxed trait errors (`Box<Error>`)](https://github.com/brson/error-chain/pull/156)
46

57
# 0.10.0
68

src/error_chain.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ macro_rules! error_chain_processed {
130130
-> $error_name
131131
where E: ::std::error::Error + Send + 'static,
132132
K: Into<$error_kind_name>
133+
{
134+
$error_name::with_boxed_chain(Box::new(error), kind)
135+
}
136+
137+
/// Construct a chained error from another boxed error and a kind, and generates a backtrace
138+
pub fn with_boxed_chain<K>(error: Box<::std::error::Error + Send>, kind: K)
139+
-> $error_name
140+
where K: Into<$error_kind_name>
133141
{
134142
$error_name(
135143
kind.into(),
136-
$crate::State::new::<$error_name>(Box::new(error), ),
144+
$crate::State::new::<$error_name>(error, ),
137145
)
138146
}
139147

@@ -302,7 +310,7 @@ macro_rules! error_chain_processed {
302310
// The ResultExt trait defines the `chain_err` method.
303311

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

315-
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
323+
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
316324
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
317325
where F: FnOnce() -> EK,
318326
EK: Into<$error_kind_name> {
@@ -323,6 +331,16 @@ macro_rules! error_chain_processed {
323331
}
324332
}
325333

334+
impl<T> $result_ext_name<T> for ::std::option::Option<T> {
335+
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
336+
where F: FnOnce() -> EK,
337+
EK: Into<$error_kind_name> {
338+
self.ok_or_else(move || {
339+
$crate::ChainedError::from_kind(callback().into())
340+
})
341+
}
342+
}
343+
326344

327345
};
328346
}

0 commit comments

Comments
 (0)