feat: improve configuration file error handling

Cleans up the error messages generated when errors are encountered in
the configuration file. Instead of showing the raw error message, give
clear information about the problem.
This commit is contained in:
Thomas Frans
2023-11-28 21:25:18 +01:00
committed by Henrik Friedrichsen
parent 0cee99ba4c
commit e0373890fe
3 changed files with 32 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{fs, process};
@@ -195,7 +196,15 @@ impl Config {
pub fn new(filename: Option<String>) -> Self {
let filename = filename.unwrap_or(CONFIGURATION_FILE_NAME.to_owned());
let values = load(&filename).unwrap_or_else(|e| {
eprintln!("could not load config: {e}");
eprint!(
"There is an error in your configuration file at {}:\n\n{e}",
user_configuration_directory()
.map(|ref mut path| {
path.push(CONFIGURATION_FILE_NAME);
path.to_string_lossy().to_string()
})
.expect("configuration directory expected but not found")
);
process::exit(1);
});
@@ -256,10 +265,14 @@ impl Config {
crate::theme::load(theme)
}
/// Reload the configuration file.
pub fn reload(&self) {
let cfg = load(&self.filename).expect("could not reload config");
*self.values.write().expect("can't writelock config values") = cfg
/// Attempt to reload the configuration from the configuration file.
///
/// This only updates the values stored in memory but doesn't perform any additional actions
/// like updating active keybindings.
pub fn reload(&self) -> Result<(), Box<dyn Error>> {
let cfg = load(&self.filename)?;
*self.values.write().unwrap() = cfg;
Ok(())
}
}