Improve error handling

This commit is contained in:
Reinout Meliesie 2025-02-12 11:34:38 +01:00
commit 2ec2fda116
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
4 changed files with 32 additions and 36 deletions

View file

@ -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 ;