From ed462ba67e8c2bf82d34c90526bb7180a6a688c4 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sat, 4 Jul 2020 20:41:55 -0400 Subject: [PATCH] better error messages --- src/config.rs | 28 ++++++++++++++++------------ src/error.rs | 6 +++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/config.rs b/src/config.rs index d89312e..47efac2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,10 +111,14 @@ pub fn get_config_data() -> Result { let mut folders = vec![PathBuf::from("/etc/")]; // Config folder - if let Some(folder) = config_dir() { folders.push(folder) } + if let Some(folder) = config_dir() { + folders.push(folder) + } // Home folder - if let Some(folder) = home_dir() { folders.push(folder) } + if let Some(folder) = home_dir() { + folders.push(folder) + } folders .iter_mut() @@ -133,12 +137,11 @@ pub fn get_config_data() -> Result { return Ok(ConfigData { path: location.clone(), file, - }) + }); } Err(e) => debug!( "Tried to read '{:?}' but failed due to error: {}", - location, - e + location, e ), } } @@ -166,8 +169,7 @@ pub fn get_config_data() -> Result { } Err(e) => debug!( "Tried to open a new file at '{:?}' but failed due to error: {}", - location, - e + location, e ), } } @@ -181,10 +183,12 @@ pub fn load_custom_path_config( path: impl Into, ) -> Result { let path = path.into(); - Ok(ConfigData { - file: OpenOptions::new().read(true).open(&path)?, - path, - }) + let file = OpenOptions::new() + .read(true) + .open(&path) + .map_err(|e| BunBunError::InvalidConfigPath(path.clone(), e))?; + + Ok(ConfigData { file, path }) } pub fn read_config(mut config_file: File) -> Result { @@ -248,4 +252,4 @@ mod route { "---\nhello world" ); } -} \ No newline at end of file +} diff --git a/src/error.rs b/src/error.rs index 807b55d..3ddb443 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,8 @@ pub enum BunBunError { WatchError(hotwatch::Error), LoggerInitError(log::SetLoggerError), CustomProgramError(String), - NoValidConfigPath + NoValidConfigPath, + InvalidConfigPath(std::path::PathBuf, std::io::Error), } impl Error for BunBunError {} @@ -23,6 +24,9 @@ impl fmt::Display for BunBunError { Self::LoggerInitError(e) => e.fmt(f), Self::CustomProgramError(msg) => write!(f, "{}", msg), Self::NoValidConfigPath => write!(f, "No valid config path was found!"), + Self::InvalidConfigPath(path, reason) => { + write!(f, "Failed to access {:?}: {}", path, reason) + } } } }