graceful bad-config handling

This commit is contained in:
Edward Shen 2019-12-21 15:56:59 -05:00
parent e11e514c12
commit 320e41c6f2
Signed by: edward
GPG key ID: F350507060ED6C90

View file

@ -21,12 +21,14 @@ static CONFIG_FILE: &str = "bunbun.toml";
#[derive(Debug)]
enum BunBunError {
IoError(std::io::Error),
ParseError(serde_yaml::Error),
}
impl fmt::Display for BunBunError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BunBunError::IoError(e) => e.fmt(f),
BunBunError::ParseError(e) => e.fmt(f),
}
}
}
@ -39,6 +41,12 @@ impl From<std::io::Error> for BunBunError {
}
}
impl From<serde_yaml::Error> for BunBunError {
fn from(error: serde_yaml::Error) -> Self {
BunBunError::ParseError(error)
}
}
#[get("/ls")]
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
let data = data.read().unwrap();
@ -162,11 +170,15 @@ fn main() -> Result<(), BunBunError> {
.watch(CONFIG_FILE, move |e: Event| {
if let Event::Write(_) = e {
let mut state = state.write().unwrap();
let conf = read_config(CONFIG_FILE).unwrap();
match read_config(CONFIG_FILE) {
Ok(conf) => {
state.public_address = conf.public_address;
state.default_route = conf.default_route;
state.routes = conf.routes;
}
Err(e) => eprintln!("Config is malformed: {}", e),
}
}
})
.expect("failed to watch");
@ -201,8 +213,7 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
File::open(config_file_path)?
}
};
// TODO: Handle parse failure
Ok(serde_yaml::from_reader(config_file).unwrap())
Ok(serde_yaml::from_reader(config_file)?)
}
fn compile_templates() -> Handlebars {