From d8ed14373e7c012bbdbef1b4dfc93af19b79e10f Mon Sep 17 00:00:00 2001 From: rasul Date: Wed, 30 Oct 2019 13:03:35 -0500 Subject: [PATCH] handle errors better to include error messages --- src/apps.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/apps.rs b/src/apps.rs index 2a3e60b..c707c1b 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,3 +1,4 @@ +use std::boxed::Box; use std::fs::File; use std::io::Read; use std::path::PathBuf; @@ -12,11 +13,29 @@ pub struct Apps { } impl Apps { - pub fn load>(p: T) -> Result { - let mut file = File::open(p.into())?; - let mut buf = String::new(); - file.read_to_string(&mut buf)?; - Ok(toml::from_str(&buf)?) + pub fn load(p: PathBuf) -> Result { + match File::open(p.into()) { + Ok(file) => { + let mut buf = String::new(); + + 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 {