handle errors better to include error messages

master
rasul 5 years ago
parent 11c3237f44
commit d8ed14373e

@ -1,3 +1,4 @@
use std::boxed::Box;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
@ -12,11 +13,29 @@ pub struct Apps {
} }
impl Apps { impl Apps {
pub fn load<T: Into<PathBuf>>(p: T) -> Result<Self> { pub fn load(p: PathBuf) -> Result<Self> {
let mut file = File::open(p.into())?; match File::open(p.into()) {
Ok(file) => {
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf)?;
Ok(toml::from_str(&buf)?) if let Err(e) = file.read_to_string(&mut buf) {
error!("unable to read apps file: {}: {:?}", p.display(), e);
return Err(Box::new(e));
};
match toml::from_str(&buf) {
Ok(t) => Ok(t),
Err(e) => {
error!("Invalid toml in apps file: {}: {:?}", p.display(), e);
Err(Box::new(e))
}
}
}
Err(e) => {
error!("Unable to open apps file: {}: {:?}", p.display(), e);
Err(Box::new(e))
}
}
} }
pub fn apps(&self) -> Vec<App> { pub fn apps(&self) -> Vec<App> {

Loading…
Cancel
Save