Improve error handling
This commit is contained in:
		
					parent
					
						
							
								795e4b20f2
							
						
					
				
			
			
				commit
				
					
						2ec2fda116
					
				
			
		
					 4 changed files with 32 additions and 36 deletions
				
			
		|  | @ -24,8 +24,8 @@ impl DataManager { | |||
| 		Ok ( Self { sqlite_client } ) | ||||
| 	} | ||||
| 
 | ||||
| 	pub async fn get_collection_overview ( & self ) -> CollectionOverview { | ||||
| 		self . sqlite_client . conn ( |sqlite_connection| { | ||||
| 	pub async fn get_collection_overview ( & self ) -> Result <CollectionOverview> { | ||||
| 		let collection_overview = self . sqlite_client . conn ( |sqlite_connection| { | ||||
| 			let films = sqlite_connection | ||||
| 				. prepare ( " | ||||
| 					select uuid , name , original_name , release_date , runtime_minutes , poster_file_path | ||||
|  | @ -48,7 +48,8 @@ impl DataManager { | |||
| 				. collect () ? ; | ||||
| 
 | ||||
| 			Ok ( CollectionOverview { films , series } ) | ||||
| 		} ) . await . unwrap () | ||||
| 		} ) . await ? ; | ||||
| 		Ok (collection_overview) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										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 ; | ||||
|  |  | |||
|  | @ -42,19 +42,19 @@ fn show_window ( application : & Application ) { | |||
| 	ui . show_window () ; | ||||
| 
 | ||||
| 	spawn_future_local ( async move { | ||||
| 		async_unit_result_context ( | ||||
| 		async_result_context ! ( | ||||
| 			async { | ||||
| 				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 ; | ||||
| 				Ok (()) | ||||
| 			} , | ||||
| 			|error| { | ||||
| 			err => |error| { | ||||
| 				match error { | ||||
| 					CollectionFileReadError => eprintln ! ("Could not read collection file") , | ||||
| 				} ; | ||||
| 				ui . close_window () ; | ||||
| 			} , | ||||
| 		) . await ; | ||||
| 		) ; | ||||
| 	} ) ; | ||||
| } | ||||
|  |  | |||
|  | @ -58,7 +58,12 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > ( | |||
| 	on_sort : & impl Fn ( A :: Sorting ) , | ||||
| 	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 () ; | ||||
| 	if sorting_property == previous_sorting . get_property () { | ||||
|  | @ -66,20 +71,20 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > ( | |||
| 			Ascending => { | ||||
| 				let new_sorting = A :: Sorting :: new ( sorting_property , Descending ) ; | ||||
| 				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) ; | ||||
| 			} , | ||||
| 			Descending => { | ||||
| 				let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; | ||||
| 				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) ; | ||||
| 			} , | ||||
| 		} | ||||
| 	} else { | ||||
| 		let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; | ||||
| 		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) ; | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue