From 02c60b23ea92566ecbe461484769b2018f14f724 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 16 Jul 2017 23:28:21 +0200 Subject: [PATCH] Improve error handling --- src/errors.rs | 12 ++++++------ src/prefs.rs | 15 +++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 4f60757b6..e50d4ce00 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -174,8 +174,8 @@ macro_rules! result_warn { ::std::result::Result::Ok(v) => Ok(v), ::std::result::Result::Err(err) => { use std::error::Error; - let warn_string = format!("{}\n\nCause: {}", err.description(), - err.cause().map(|e| e.description()).unwrap_or("")); + let warn_string = format!("{}{}", err.description(), + err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from(""))); warn!("{}", warn_string); error_dialog!(warn_string.as_str(), $parent); Err(err) @@ -192,8 +192,8 @@ macro_rules! wresult_warn { ::w_result::WResult::WOk(t, ws) => { use std::error::Error; for w in ws { - let warn_string = format!("{}\n\nCause: {}", w.description(), - w.cause().map(|e| e.description()).unwrap_or("")); + let warn_string = format!("{}{}", w.description(), + w.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from(""))); warn!("{}", warn_string); error_dialog!(warn_string.as_str(), $parent); } @@ -223,8 +223,8 @@ macro_rules! unwrap_error { ::std::result::Result::Ok(v) => v, ::std::result::Result::Err(err) => { use std::error::Error; - let err_string = format!("{}\n\nCause: {}", err.description(), - err.cause().map(|e| e.description()).unwrap_or("")); + let err_string = format!("{}{}", err.description(), + err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from(""))); error!("{}", err_string); error_dialog!(err_string.as_str(), $parent); diff --git a/src/prefs.rs b/src/prefs.rs index 518dac8b3..12e428a34 100644 --- a/src/prefs.rs +++ b/src/prefs.rs @@ -250,14 +250,21 @@ impl Prefs { /// Store the current preferences to the config file. pub fn store_config(&self) -> Result<()> { let config_path = get_xdg_dirs().place_config_file("pnmixer.toml") - .from_err()?; + .chain_err(|| { + format!("Could not create config directory at {:?}", + get_xdg_dirs().get_config_home()) + })?; debug!("Storing config in {:?}", config_path); - let mut f = File::create(config_path) - .chain_err(|| "Could not open/create config file for writing")?; + let mut f = File::create(config_path.clone()) + .chain_err(|| format!("Could not open/create config file {:?} for writing", + config_path))?; f.write_all(self.to_str().as_bytes()) - .chain_err(|| "Could not write to config file")?; + .chain_err(|| { + format!("Could not write to config file {:?}", + config_path) + })?; return Ok(()); }