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