Improve error handling
This commit is contained in:
parent
795e4b20f2
commit
2ec2fda116
4 changed files with 32 additions and 36 deletions
40
src/error.rs
40
src/error.rs
|
|
@ -1,6 +1,5 @@
|
|||
use std :: * ;
|
||||
use std :: any :: * ;
|
||||
use std :: future :: * ;
|
||||
|
||||
|
||||
|
||||
|
|
@ -16,28 +15,19 @@ impl From < Box < dyn Any + Send > > for ZoodexError {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn async_result_context <
|
||||
AsyncFunction : Future < Output = Result <Success> > ,
|
||||
SuccessHandler : Fn (Success) ,
|
||||
FailureHandler : Fn (ZoodexError) ,
|
||||
Success ,
|
||||
> (
|
||||
function : AsyncFunction ,
|
||||
on_success : SuccessHandler ,
|
||||
on_failure : FailureHandler ,
|
||||
) {
|
||||
match function . await {
|
||||
Ok (value) => on_success (value) ,
|
||||
Err (error) => on_failure (error) ,
|
||||
} ;
|
||||
}
|
||||
macro_rules ! async_result_context { (
|
||||
$ future : expr
|
||||
$ ( , ok => $ on_success : expr ) ?
|
||||
$ ( , err => $ on_failure : expr ) ? $ (,) ?
|
||||
) => {
|
||||
# [ allow (unreachable_patterns) ] match $ future . await {
|
||||
$ ( Ok (value) => $ on_success (value) , ) ?
|
||||
Ok (_) => {} ,
|
||||
$ ( Err (error) => $ on_failure (error) , ) ?
|
||||
Err (_) => {} ,
|
||||
}
|
||||
} }
|
||||
|
||||
pub async fn async_unit_result_context <
|
||||
AsyncFunction : Future < Output = Result <()> > ,
|
||||
FailureHandler : Fn (ZoodexError) ,
|
||||
> (
|
||||
function : AsyncFunction ,
|
||||
on_failure : FailureHandler ,
|
||||
) {
|
||||
async_result_context ( function , |_| () , on_failure ) . await ;
|
||||
}
|
||||
|
||||
|
||||
pub (crate) use async_result_context ;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue