pub fn save_settings(&self) {
if let Err(error) = self.actually_save_settings_with_error() {
eprintln!("Failed to save ui_settings:");
eprintln!("{:?}", error);
}
}
fn actually_save_settings_with_error(&self) -> Result<(), Box<dyn Error>> {
serde_json::to_writer(&File::create(SETTINGS_LOCK_FILE_NAME)?, &self)?;
Ok(())
}
#How do you consistently error handle stuff like this
4 messages · Page 1 of 1 (latest)
If I write it in one function, then I have to unwrap and take care of the ? operator.
I could do this:
pub fn save_settings(&self) {
fn actually_save_settings_with_error(u_self: &UISettings) -> Result<(), Box<dyn Error>> {
serde_json::to_writer(&File::create(SETTINGS_LOCK_FILE_NAME)?, &u_self)?;
Ok(())
}
if let Err(error) = actually_save_settings_with_error(&self) {
eprintln!("Failed to save ui_settings:");
eprintln!("{:?}", error);
}
}
To really signify that the function actually_save_settings_with_error is only to be used in save_settings, but now I can't use self or Self keywords
I believe it's common to have them both in the impl, pub fn save_settings and fn try_save_settings