Improve error handling

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

View file

@ -24,8 +24,8 @@ impl DataManager {
Ok ( Self { sqlite_client } ) Ok ( Self { sqlite_client } )
} }
pub async fn get_collection_overview ( & self ) -> CollectionOverview { pub async fn get_collection_overview ( & self ) -> Result <CollectionOverview> {
self . sqlite_client . conn ( |sqlite_connection| { let collection_overview = self . sqlite_client . conn ( |sqlite_connection| {
let films = sqlite_connection let films = sqlite_connection
. prepare ( " . prepare ( "
select uuid , name , original_name , release_date , runtime_minutes , poster_file_path select uuid , name , original_name , release_date , runtime_minutes , poster_file_path
@ -48,7 +48,8 @@ impl DataManager {
. collect () ? ; . collect () ? ;
Ok ( CollectionOverview { films , series } ) Ok ( CollectionOverview { films , series } )
} ) . await . unwrap () } ) . await ? ;
Ok (collection_overview)
} }
} }

View file

@ -1,6 +1,5 @@
use std :: * ; use std :: * ;
use std :: any :: * ; use std :: any :: * ;
use std :: future :: * ;
@ -16,28 +15,19 @@ impl From < Box < dyn Any + Send > > for ZoodexError {
} }
} }
pub async fn async_result_context < macro_rules ! async_result_context { (
AsyncFunction : Future < Output = Result <Success> > , $ future : expr
SuccessHandler : Fn (Success) , $ ( , ok => $ on_success : expr ) ?
FailureHandler : Fn (ZoodexError) , $ ( , err => $ on_failure : expr ) ? $ (,) ?
Success , ) => {
> ( # [ allow (unreachable_patterns) ] match $ future . await {
function : AsyncFunction , $ ( Ok (value) => $ on_success (value) , ) ?
on_success : SuccessHandler , Ok (_) => {} ,
on_failure : FailureHandler , $ ( Err (error) => $ on_failure (error) , ) ?
) { Err (_) => {} ,
match function . await { }
Ok (value) => on_success (value) , } }
Err (error) => on_failure (error) ,
} ;
}
pub async fn async_unit_result_context <
AsyncFunction : Future < Output = Result <()> > ,
FailureHandler : Fn (ZoodexError) , pub (crate) use async_result_context ;
> (
function : AsyncFunction ,
on_failure : FailureHandler ,
) {
async_result_context ( function , |_| () , on_failure ) . await ;
}

View file

@ -42,19 +42,19 @@ fn show_window ( application : & Application ) {
ui . show_window () ; ui . show_window () ;
spawn_future_local ( async move { spawn_future_local ( async move {
async_unit_result_context ( async_result_context ! (
async { async {
let data_manager = DataManager :: new () . await ? ; let data_manager = DataManager :: new () . await ? ;
let collection = data_manager . get_collection_overview () . await ; let collection = data_manager . get_collection_overview () . await ? ;
ui . render_collection_overview (collection) . await ; ui . render_collection_overview (collection) . await ;
Ok (()) Ok (())
} , } ,
|error| { err => |error| {
match error { match error {
CollectionFileReadError => eprintln ! ("Could not read collection file") , CollectionFileReadError => eprintln ! ("Could not read collection file") ,
} ; } ;
ui . close_window () ; ui . close_window () ;
} , } ,
) . await ; ) ;
} ) ; } ) ;
} }

View file

@ -58,7 +58,12 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > (
on_sort : & impl Fn ( A :: Sorting ) , on_sort : & impl Fn ( A :: Sorting ) ,
sort_icons : & [ Image ] , sort_icons : & [ Image ] ,
) { ) {
let ( sorting_property , _ ) = A :: get_property_descriptions () [ row as usize ] . clone () ; // TODO: Bounds checking let row = row as usize ;
debug_assert ! (
row <= A :: get_property_descriptions () . len () ,
"Sorting menu has more rows than media adapter has property descriptions" ,
) ;
let ( sorting_property , _ ) = A :: get_property_descriptions () [row] . clone () ;
let previous_sorting = * previous_sorting_mut . borrow () ; let previous_sorting = * previous_sorting_mut . borrow () ;
if sorting_property == previous_sorting . get_property () { if sorting_property == previous_sorting . get_property () {
@ -66,20 +71,20 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > (
Ascending => { Ascending => {
let new_sorting = A :: Sorting :: new ( sorting_property , Descending ) ; let new_sorting = A :: Sorting :: new ( sorting_property , Descending ) ;
previous_sorting_mut . replace (new_sorting) ; previous_sorting_mut . replace (new_sorting) ;
sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-descending-symbolic") ) ; sort_icons [row] . set_icon_name ( Some ("view-sort-descending-symbolic") ) ;
on_sort (new_sorting) ; on_sort (new_sorting) ;
} , } ,
Descending => { Descending => {
let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ;
previous_sorting_mut . replace (new_sorting) ; previous_sorting_mut . replace (new_sorting) ;
sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; sort_icons [row] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ;
on_sort (new_sorting) ; on_sort (new_sorting) ;
} , } ,
} }
} else { } else {
let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ;
previous_sorting_mut . replace (new_sorting) ; previous_sorting_mut . replace (new_sorting) ;
sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; sort_icons [row] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ;
on_sort (new_sorting) ; on_sort (new_sorting) ;
} }
} }