I am trying to learn how to better perform error handling. In an iced project in the new function I create a task that runs 2 async functions. They have to run in series which I think eliminates the ability to use of the join macro. The code works but I seem to be stuck error handling with match ok/err statements. Is there a better way to deal with the error handling? I should note that both async functions return a result. Any assistance will be appreciated.
fn new() -> (Self, Task<Message>) {
let initial_state = Self {
screen: Screen::LoadingScreen,
start_date_90: "".to_string(),
start_date_180: "".to_string(),
start_date_365: "".to_string(),
start_date_3650: "".to_string(),
};
let initial_task = Task::future(async {
use iced::futures::TryFutureExt;
let handle1 = tokio::spawn( // Result<bool, join error> assigned to handle1
insert_csv_to_sql()
.unwrap_or_else(|error| {
println!("Error inserting CSV data: {}", error);
false
})
);
let result1 = match handle1.await {
Ok(val) => val,
Err(e) => {
println!("Join error: {}", e);
false
},
};
let mut result2: Vec<String> = Vec::new();
if result1 == true {
let handle2 = tokio::spawn(
get_start_dates()
.unwrap_or_else(|error| {
println!("Error getting start dates: {}", error);
Vec::new()
})
);
result2 = match handle2.await {
Ok(val) => val,
Err(e) => {
println!("Join error: {}", e);
Vec::new()
},
};
}
Message::UpdateStartDates(result2)
});
(initial_state, initial_task)
}