better error messages
This commit is contained in:
parent
4226b75f18
commit
ed462ba67e
2 changed files with 21 additions and 13 deletions
|
@ -111,10 +111,14 @@ pub fn get_config_data() -> Result<ConfigData, BunBunError> {
|
||||||
let mut folders = vec![PathBuf::from("/etc/")];
|
let mut folders = vec![PathBuf::from("/etc/")];
|
||||||
|
|
||||||
// Config folder
|
// Config folder
|
||||||
if let Some(folder) = config_dir() { folders.push(folder) }
|
if let Some(folder) = config_dir() {
|
||||||
|
folders.push(folder)
|
||||||
|
}
|
||||||
|
|
||||||
// Home folder
|
// Home folder
|
||||||
if let Some(folder) = home_dir() { folders.push(folder) }
|
if let Some(folder) = home_dir() {
|
||||||
|
folders.push(folder)
|
||||||
|
}
|
||||||
|
|
||||||
folders
|
folders
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
|
@ -133,12 +137,11 @@ pub fn get_config_data() -> Result<ConfigData, BunBunError> {
|
||||||
return Ok(ConfigData {
|
return Ok(ConfigData {
|
||||||
path: location.clone(),
|
path: location.clone(),
|
||||||
file,
|
file,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
Err(e) => debug!(
|
Err(e) => debug!(
|
||||||
"Tried to read '{:?}' but failed due to error: {}",
|
"Tried to read '{:?}' but failed due to error: {}",
|
||||||
location,
|
location, e
|
||||||
e
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,8 +169,7 @@ pub fn get_config_data() -> Result<ConfigData, BunBunError> {
|
||||||
}
|
}
|
||||||
Err(e) => debug!(
|
Err(e) => debug!(
|
||||||
"Tried to open a new file at '{:?}' but failed due to error: {}",
|
"Tried to open a new file at '{:?}' but failed due to error: {}",
|
||||||
location,
|
location, e
|
||||||
e
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,10 +183,12 @@ pub fn load_custom_path_config(
|
||||||
path: impl Into<PathBuf>,
|
path: impl Into<PathBuf>,
|
||||||
) -> Result<ConfigData, BunBunError> {
|
) -> Result<ConfigData, BunBunError> {
|
||||||
let path = path.into();
|
let path = path.into();
|
||||||
Ok(ConfigData {
|
let file = OpenOptions::new()
|
||||||
file: OpenOptions::new().read(true).open(&path)?,
|
.read(true)
|
||||||
path,
|
.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> {
|
pub fn read_config(mut config_file: File) -> Result<Config, BunBunError> {
|
||||||
|
@ -248,4 +252,4 @@ mod route {
|
||||||
"---\nhello world"
|
"---\nhello world"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,8 @@ pub enum BunBunError {
|
||||||
WatchError(hotwatch::Error),
|
WatchError(hotwatch::Error),
|
||||||
LoggerInitError(log::SetLoggerError),
|
LoggerInitError(log::SetLoggerError),
|
||||||
CustomProgramError(String),
|
CustomProgramError(String),
|
||||||
NoValidConfigPath
|
NoValidConfigPath,
|
||||||
|
InvalidConfigPath(std::path::PathBuf, std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for BunBunError {}
|
impl Error for BunBunError {}
|
||||||
|
@ -23,6 +24,9 @@ impl fmt::Display for BunBunError {
|
||||||
Self::LoggerInitError(e) => e.fmt(f),
|
Self::LoggerInitError(e) => e.fmt(f),
|
||||||
Self::CustomProgramError(msg) => write!(f, "{}", msg),
|
Self::CustomProgramError(msg) => write!(f, "{}", msg),
|
||||||
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
|
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
|
||||||
|
Self::InvalidConfigPath(path, reason) => {
|
||||||
|
write!(f, "Failed to access {:?}: {}", path, reason)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue