Browse Source

Additional logging for ConfigError

develop
Drew Short 6 years ago
parent
commit
822311397d
  1. 27
      src/config.rs
  2. 5
      src/main.rs

27
src/config.rs

@ -79,13 +79,15 @@ fn get_default_config() -> Config {
#[derive(Debug)] #[derive(Debug)]
pub struct ConfigError { pub struct ConfigError {
description: String
description: String,
original_error: Option<Box<Error>>
} }
impl ConfigError { impl ConfigError {
fn new(description: &str) -> ConfigError {
fn new(description: &str, original_error: Option<Box<Error>>) -> ConfigError {
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 { impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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<Config, ConfigError> { fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
match serde_yaml::from_str(yaml_str) { match serde_yaml::from_str(yaml_str) {
Ok(v) => Result::Ok(v), 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<Config, ConfigError> {
if c > 0 { if c > 0 {
read_config(&contents) read_config(&contents)
} else { } 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))))
} }
} }

5
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_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
let config = match config::load_config(config_path) { 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) => { Err(e) => {
error!("{}", e); error!("{}", e);
Option::None Option::None

Loading…
Cancel
Save