diff --git a/src/config.rs b/src/config.rs index 482d9e5..8fd41c1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -79,13 +79,15 @@ fn get_default_config() -> Config { #[derive(Debug)] pub struct ConfigError { - description: String + description: String, + original_error: Option> } impl ConfigError { - fn new(description: &str) -> ConfigError { + fn new(description: &str, original_error: Option>) -> ConfigError { ConfigError { - description: String::from(description) + description: String::from(description), + original_error } } } @@ -94,15 +96,19 @@ impl Error for ConfigError {} impl fmt::Display for ConfigError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Configuration Error: {}", self.description) + match &self.original_error { + Some(original_error) => { + write!(f, "{}: \"{}\"", self.description, original_error) + }, + None => write!(f, "{}", self.description) + } } } fn read_config(yaml_str: &str) -> Result { match serde_yaml::from_str(yaml_str) { Ok(v) => Result::Ok(v), - // File wasn't valid Config/YAML - Err(_e) => Result::Err(ConfigError::new("Configuration file was invalid")) + Err(e) => Result::Err(ConfigError::new("Invalid Configuration", Option::Some(Box::from(e)))) } } @@ -115,15 +121,12 @@ pub fn load_config(path: &str) -> Result { if c > 0 { read_config(&contents) } else { - // File was empty - Result::Err(ConfigError::new("Configuration file was empty")) + Result::Err(ConfigError::new("Empty Configuration File", Option::None)) } }, - // File couldn't be read - Err(_e) => Result::Err(ConfigError::new("Configuration file could not be read")) + Err(e) => Result::Err(ConfigError::new("Cannot Read Configuration File", Option::Some(Box::from(e)))) } }, - // File Doesn't exist - Err(_e) => Result::Err(ConfigError::new(&format!("Configuration file doesn't exist \"{}\"", path))) + Err(e) => Result::Err(ConfigError::new(&format!("Configuration File Doesn't Exist \"{}\"", path), Option::Some(Box::from(e)))) } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4eb8696..b2f4eba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,10 @@ fn main() { let config_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml"); let config = match config::load_config(config_path) { - Ok(c) => Option::Some(c), + Ok(c) => { + info!("Loaded configuration from \"{}\"", config_path); + Option::Some(c) + }, Err(e) => { error!("{}", e); Option::None