From 4849cc4822dd7171ec9426a57abc284625f0a137 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Sep 2017 22:04:32 +0200 Subject: [PATCH 1/4] Remove TemplateStringRegistrationError by linking in handlebars error type --- lib/core/libimagrt/src/error.rs | 9 ++++----- lib/core/libimagrt/src/logger.rs | 15 +++++---------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index 3ecca79e..495d5863 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -22,6 +22,10 @@ error_chain! { RuntimeError, RuntimeErrorKind, ResultExt, Result; } + foreign_links { + HandlebarsTemplateError(::handlebars::TemplateError); + } + errors { Instantiate { description("Could not instantiate") @@ -73,11 +77,6 @@ error_chain! { display("Error while reading in TOML document") } - TemplateStringRegistrationError { - description("Error while registering logging template string") - display("Error while registering logging template string") - } - ConfigMissingLoggingFormatTrace { description("Missing config for logging format for trace logging") display("Missing config for logging format for trace logging") diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index 8785e27b..e4dc6670 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -93,28 +93,23 @@ impl ImagLogger { { let fmt = try!(aggregate_global_format_trace(matches, config)); - try!(handlebars.register_template_string("TRACE", fmt) // name must be uppercase - .chain_err(|| EK::TemplateStringRegistrationError)); + try!(handlebars.register_template_string("TRACE", fmt)); // name must be uppercase } { let fmt = try!(aggregate_global_format_debug(matches, config)); - try!(handlebars.register_template_string("DEBUG", fmt) // name must be uppercase - .chain_err(|| EK::TemplateStringRegistrationError)); + try!(handlebars.register_template_string("DEBUG", fmt)); // name must be uppercase } { let fmt = try!(aggregate_global_format_info(matches, config)); - try!(handlebars.register_template_string("INFO", fmt) // name must be uppercase - .chain_err(|| EK::TemplateStringRegistrationError)); + try!(handlebars.register_template_string("INFO", fmt)); // name must be uppercase } { let fmt = try!(aggregate_global_format_warn(matches, config)); - try!(handlebars.register_template_string("WARN", fmt) // name must be uppercase - .chain_err(|| EK::TemplateStringRegistrationError)); + try!(handlebars.register_template_string("WARN", fmt)); // name must be uppercase } { let fmt = try!(aggregate_global_format_error(matches, config)); - try!(handlebars.register_template_string("ERROR", fmt) // name must be uppercase - .chain_err(|| EK::TemplateStringRegistrationError)); + try!(handlebars.register_template_string("ERROR", fmt)); // name must be uppercase } Ok(ImagLogger { From a015b07f6abe3b9fd327513f95ab9fe07929a1bd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Sep 2017 22:09:36 +0200 Subject: [PATCH 2/4] Remove TomlReadError by linking in ::toml_query::error::Error --- lib/core/libimagrt/src/error.rs | 6 +----- lib/core/libimagrt/src/logger.rs | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index 495d5863..86fe7652 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -23,6 +23,7 @@ error_chain! { } foreign_links { + TomlError(::toml_query::error::Error); HandlebarsTemplateError(::handlebars::TemplateError); } @@ -72,11 +73,6 @@ error_chain! { display("Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed") } - TomlReadError { - description("Error while reading in TOML document") - display("Error while reading in TOML document") - } - ConfigMissingLoggingFormatTrace { description("Missing config for logging format for trace logging") display("Missing config for logging format for trace logging") diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index e4dc6670..7a7b8186 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -374,21 +374,21 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some), Ok(None) => Ok(None), Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).chain_err(|| EK::TomlReadError), + Err(e) => Err(e).map_err(From::from), }); let level = try!(match v.read("level") { Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some), Ok(None) => Ok(None), Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).chain_err(|| EK::TomlReadError), + Err(e) => Err(e).map_err(From::from), }); let enabled = try!(match v.read("enabled") { Ok(Some(&Value::Boolean(b))) => Ok(b), Ok(None) => Ok(false), Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).chain_err(|| EK::TomlReadError), + Err(e) => Err(e).map_err(From::from), }); let module_settings = ModuleSettings { From 307165d1b29c63200bd7da4738463fe147acb7f5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Sep 2017 22:15:18 +0200 Subject: [PATCH 3/4] Remove ConfigReadError because ::toml_query::error::Error is now linked in --- lib/core/libimagrt/src/error.rs | 5 -- lib/core/libimagrt/src/logger.rs | 126 ++++++++++++++----------------- 2 files changed, 57 insertions(+), 74 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index 86fe7652..1bf3d150 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -48,11 +48,6 @@ error_chain! { display("IO Error: Could not open logfile") } - ConfigReadError { - description("Error while reading the configuration") - display("Error while reading the configuration") - } - ConfigTypeError { description("Error while reading the configuration: Type Error") display("Error while reading the configuration: Type Error") diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index 7a7b8186..30a1e53d 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -195,15 +195,12 @@ fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Configuration -> Result { match config { - Some(cfg) => match cfg - .read("imag.logging.level") - .chain_err(|| EK::ConfigReadError) - { - Ok(Some(&Value::String(ref s))) => match_log_level_str(s), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)), - Err(e) => Err(e) - }, + Some(cfg) => match cfg.read("imag.logging.level") { + Ok(Some(&Value::String(ref s))) => match_log_level_str(s), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)), + Err(e) => Err(e).map_err(From::from), + }, None => { if matches.is_present(Runtime::arg_debugging_name()) { return Ok(LogLevel::Debug) @@ -253,15 +250,12 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Configura { match config { - Some(cfg) => match cfg - .read("imag.logging.destinations") - .chain_err(|| EK::ConfigReadError) - { - Ok(Some(&Value::Array(ref a))) => translate_destinations(a), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Ok(None) => Err(RE::from_kind(EK::GlobalDestinationConfigMissing)), - Err(e) => Err(e) - }, + Some(cfg) => match cfg.read("imag.logging.destinations") { + Ok(Some(&Value::Array(ref a))) => translate_destinations(a), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(None) => Err(RE::from_kind(EK::GlobalDestinationConfigMissing)), + Err(e) => Err(e).map_err(From::from), + }, None => { if let Some(values) = matches.value_of(Runtime::arg_logdest_name()) { // parse logdest specification from commandline @@ -291,15 +285,12 @@ fn aggregate_global_format( -> Result { match config { - Some(cfg) => match cfg - .read(read_str) - .chain_err(|| EK::ConfigReadError) - { - Ok(Some(&Value::String(ref s))) => Ok(s.clone()), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Ok(None) => Err(RE::from_kind(error_kind_if_missing)), - Err(e) => Err(e) - }, + Some(cfg) => match cfg.read(read_str) { + Ok(Some(&Value::String(ref s))) => Ok(s.clone()), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(None) => Err(RE::from_kind(error_kind_if_missing)), + Err(e) => Err(e).map_err(From::from), + }, None => match matches.value_of(cli_match_name).map(String::from) { Some(s) => Ok(s), None => Err(RE::from_kind(error_kind_if_missing)) @@ -361,55 +352,52 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio -> Result> { match config { - Some(cfg) => match cfg - .read("imag.logging.modules") - .chain_err(|| EK::ConfigReadError) - { - Ok(Some(&Value::Table(ref t))) => { - // translate the module settings from the table `t` - let mut settings = BTreeMap::new(); + Some(cfg) => match cfg.read("imag.logging.modules") { + Ok(Some(&Value::Table(ref t))) => { + // translate the module settings from the table `t` + let mut settings = BTreeMap::new(); - for (module_name, v) in t { - let destinations = try!(match v.read("destinations") { - Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some), - Ok(None) => Ok(None), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).map_err(From::from), - }); + for (module_name, v) in t { + let destinations = try!(match v.read("destinations") { + Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some), + Ok(None) => Ok(None), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Err(e) => Err(e).map_err(From::from), + }); - let level = try!(match v.read("level") { - Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some), - Ok(None) => Ok(None), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).map_err(From::from), - }); + let level = try!(match v.read("level") { + Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some), + Ok(None) => Ok(None), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Err(e) => Err(e).map_err(From::from), + }); - let enabled = try!(match v.read("enabled") { - Ok(Some(&Value::Boolean(b))) => Ok(b), - Ok(None) => Ok(false), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Err(e) => Err(e).map_err(From::from), - }); + let enabled = try!(match v.read("enabled") { + Ok(Some(&Value::Boolean(b))) => Ok(b), + Ok(None) => Ok(false), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Err(e) => Err(e).map_err(From::from), + }); - let module_settings = ModuleSettings { - enabled: enabled, - level: level, - destinations: destinations, - }; + let module_settings = ModuleSettings { + enabled: enabled, + level: level, + destinations: destinations, + }; - // We don't care whether there was a value, we override it. - let _ = settings.insert(module_name.to_owned(), module_settings); - } + // We don't care whether there was a value, we override it. + let _ = settings.insert(module_name.to_owned(), module_settings); + } - Ok(settings) - }, - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), - Ok(None) => { - // No modules configured. This is okay! - Ok(BTreeMap::new()) - }, - Err(e) => Err(e), + Ok(settings) }, + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(None) => { + // No modules configured. This is okay! + Ok(BTreeMap::new()) + }, + Err(e) => Err(e).map_err(From::from), + }, None => { write!(stderr(), "No Configuration.").ok(); write!(stderr(), "cannot find module-settings for logging.").ok(); From 126aa75a2c3a8c0405dbaad178590c6a8e0b5c88 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Sep 2017 22:24:58 +0200 Subject: [PATCH 4/4] Add param to ConfigTypeError --- lib/core/libimagrt/src/error.rs | 4 +-- lib/core/libimagrt/src/logger.rs | 44 ++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index 1bf3d150..a0e32d1b 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -48,9 +48,9 @@ error_chain! { display("IO Error: Could not open logfile") } - ConfigTypeError { + ConfigTypeError(path: String, should_be_type: &'static str) { description("Error while reading the configuration: Type Error") - display("Error while reading the configuration: Type Error") + display("Type Error: '{}' should be '{}'", path, should_be_type) } GlobalLogLevelConfigMissing { diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index 30a1e53d..8a50d90d 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -197,7 +197,11 @@ fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Configuration match config { Some(cfg) => match cfg.read("imag.logging.level") { Ok(Some(&Value::String(ref s))) => match_log_level_str(s), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.level".to_owned(); + let ty = "String"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)), Err(e) => Err(e).map_err(From::from), }, @@ -237,7 +241,11 @@ fn translate_destinations(raw: &Vec) -> Result> { acc.and_then(|mut v| { let dest = match *val { Value::String(ref s) => try!(translate_destination(s)), - _ => return Err(RE::from_kind(EK::ConfigTypeError)), + _ => { + let path = "imag.logging.modules..destinations".to_owned(); + let ty = "Array"; + return Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, }; v.push(dest); Ok(v) @@ -252,7 +260,11 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Configura match config { Some(cfg) => match cfg.read("imag.logging.destinations") { Ok(Some(&Value::Array(ref a))) => translate_destinations(a), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.destinations".to_owned(); + let ty = "Array"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Ok(None) => Err(RE::from_kind(EK::GlobalDestinationConfigMissing)), Err(e) => Err(e).map_err(From::from), }, @@ -287,7 +299,7 @@ fn aggregate_global_format( match config { Some(cfg) => match cfg.read(read_str) { Ok(Some(&Value::String(ref s))) => Ok(s.clone()), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError(read_str.to_owned(), "String"))), Ok(None) => Err(RE::from_kind(error_kind_if_missing)), Err(e) => Err(e).map_err(From::from), }, @@ -361,21 +373,33 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio let destinations = try!(match v.read("destinations") { Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some), Ok(None) => Ok(None), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.modules..destinations".to_owned(); + let ty = "Array"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Err(e) => Err(e).map_err(From::from), }); let level = try!(match v.read("level") { Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some), Ok(None) => Ok(None), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.modules..level".to_owned(); + let ty = "String"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Err(e) => Err(e).map_err(From::from), }); let enabled = try!(match v.read("enabled") { Ok(Some(&Value::Boolean(b))) => Ok(b), Ok(None) => Ok(false), - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.modules..enabled".to_owned(); + let ty = "Boolean"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Err(e) => Err(e).map_err(From::from), }); @@ -391,7 +415,11 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio Ok(settings) }, - Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)), + Ok(Some(_)) => { + let path = "imag.logging.modules".to_owned(); + let ty = "Table"; + Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + }, Ok(None) => { // No modules configured. This is okay! Ok(BTreeMap::new())