Introduce error handling
This commit is contained in:
parent
5d9893d304
commit
32c1d2ab67
4 changed files with 54 additions and 7 deletions
31
src/error.rs
Normal file
31
src/error.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use std :: { * , any :: * , future :: * } ;
|
||||
|
||||
|
||||
|
||||
# [ derive (Debug) ] pub enum ZoodexError {
|
||||
CollectionFileError ,
|
||||
}
|
||||
|
||||
pub type Result <Success> = result :: Result < Success , ZoodexError > ;
|
||||
|
||||
impl From < Box < dyn Any + Send > > for ZoodexError {
|
||||
fn from ( error : Box < dyn Any + Send > ) -> Self {
|
||||
* error . downcast () . unwrap ()
|
||||
}
|
||||
}
|
||||
|
||||
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) ,
|
||||
} ;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue