diff options
| author | David Lönnhager <david.l@mullvad.net> | 2020-05-08 13:16:33 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2020-05-11 11:23:13 +0200 |
| commit | 86f1cd61c04f9b008a9c04be1e8cbc08c46b3e90 (patch) | |
| tree | 8338b8f887bbcb568fbdd473091b1ea5807e9061 | |
| parent | afd52a16772f144589c7e133a509c02d55619e8b (diff) | |
| download | mullvadvpn-86f1cd61c04f9b008a9c04be1e8cbc08c46b3e90.tar.xz mullvadvpn-86f1cd61c04f9b008a9c04be1e8cbc08c46b3e90.zip | |
Handle different errors when loading settings
| -rw-r--r-- | mullvad-daemon/src/settings.rs | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs index e70229149d..e1f31ea017 100644 --- a/mullvad-daemon/src/settings.rs +++ b/mullvad-daemon/src/settings.rs @@ -31,10 +31,16 @@ pub enum Error { WriteError(String, #[error(source)] io::Error), } -#[derive(Debug)] +#[derive(err_derive::Error, Debug)] enum LoadSettingsError { + #[error(display = "Cannot find settings file")] FileNotFound, - Other, + + #[error(display = "Unable to read settings file")] + Other(#[error(source)] io::Error), + + #[error(display = "Unable to parse settings file")] + ParseError(#[error(source)] mullvad_types::settings::Error), } @@ -79,8 +85,11 @@ impl SettingsPersister { } _ => Err(error), }) - .unwrap_or_else(|_| { - info!("Failed to load settings, using defaults"); + .unwrap_or_else(|error| { + info!( + "{}", + error.display_chain_with_msg("Failed to load settings. Using defaults.") + ); (Settings::default(), true) }) } @@ -88,21 +97,20 @@ impl SettingsPersister { fn load_settings_from_file(path: &Path) -> Result<(Settings, bool), LoadSettingsError> { info!("Loading settings from {}", path.display()); - let settings_bytes = fs::read(path).map_err(|error| match error.kind() { - io::ErrorKind::NotFound => LoadSettingsError::FileNotFound, - _ => LoadSettingsError::Other + let settings_bytes = fs::read(path).map_err(|error| { + if error.kind() == io::ErrorKind::NotFound { + LoadSettingsError::FileNotFound + } else { + LoadSettingsError::Other(error) + } })?; Settings::load_from_bytes(&settings_bytes) .map(|settings| (settings, false)) - .or_else(|error| { - log::error!( - "{}", - error.display_chain_with_msg("Failed to parse settings file") - ); + .or_else(|_| { Settings::migrate_from_bytes(&settings_bytes).map(|settings| (settings, true)) }) - .map_err(|_| LoadSettingsError::Other) + .map_err(LoadSettingsError::ParseError) } #[cfg(windows)] |
