Skip to content

Commit

Permalink
src: settings: Unify error treatment to avoid panic
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Apr 28, 2023
1 parent 093e2f2 commit c232a8b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/settings/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,13 @@ fn fallback_settings_with_backup_file(file_name: &str) -> SettingsStruct {
}

fn load_settings_from_file(file_name: &str) -> SettingsStruct {
let result = std::fs::read_to_string(file_name);
if let Err(error) = result {
error!("Error loading settings file {file_name:?}. Reason: {error:?}");
return fallback_settings_with_backup_file(file_name);
};

serde_json::from_str(result.unwrap().as_str()).unwrap_or_else(|error| {
error!("Failed to load settings file {file_name:?}. Reason: {error}");
fallback_settings_with_backup_file(file_name)
})
std::fs::read_to_string(file_name)
.map_err(Error::msg)
.and_then(|value| serde_json::from_str(&value).map_err(Error::msg))
.unwrap_or_else(|error| {
error!("Failed to load settings file {file_name:?}. Reason: {error}");
fallback_settings_with_backup_file(file_name)
})
}

fn save_settings_to_file(file_name: &str, content: &SettingsStruct) -> Result<()> {
Expand Down

0 comments on commit c232a8b

Please sign in to comment.