better error messages

This commit is contained in:
Edward Shen 2020-07-04 20:41:55 -04:00
parent 4226b75f18
commit ed462ba67e
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 21 additions and 13 deletions

View file

@ -111,10 +111,14 @@ pub fn get_config_data() -> Result<ConfigData, BunBunError> {
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<ConfigData, BunBunError> {
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<ConfigData, BunBunError> {
}
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<PathBuf>,
) -> Result<ConfigData, BunBunError> {
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<Config, BunBunError> {

View file

@ -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)
}
}
}
}