From 764d72a61a54810f0c45f0a665c3cbd591b3625f Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 21 Nov 2014 09:07:08 +0100 Subject: [PATCH] Improve error messages for the config parser refs #7699 --- lib/cli/daemoncommand.cpp | 30 ++++++++++++++++++++++-------- lib/config/expression.cpp | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/cli/daemoncommand.cpp b/lib/cli/daemoncommand.cpp index 966af8568..5f1ac0513 100644 --- a/lib/cli/daemoncommand.cpp +++ b/lib/cli/daemoncommand.cpp @@ -62,14 +62,28 @@ static String LoadAppType(const String& typeSpec) return typeSpec.SubStr(index + 1); } +static void ExecuteExpression(Expression *expression) +{ + Dictionary::Ptr context = new Dictionary(); + + try { + expression->Evaluate(context); + } catch (const ConfigError& ex) { + const DebugInfo *di = boost::get_error_info(ex); + ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo()); + } catch (const std::exception& ex) { + ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex)); + } +} + static void IncludeZoneDirRecursive(const String& path) { String zoneName = Utility::BaseName(path); std::vector expressions; Utility::GlobRecursive(path, "*.conf", boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName), GlobFile); - Dictionary::Ptr context = new Dictionary(); - DictExpression(expressions).Evaluate(context); + DictExpression expr(expressions); + ExecuteExpression(&expr); } static void IncludeNonLocalZone(const String& zonePath) @@ -93,13 +107,13 @@ static bool LoadConfigFiles(const boost::program_options::variables_map& vm, con if (vm.count("config") > 0) { BOOST_FOREACH(const String& configPath, vm["config"].as >()) { Expression *expression = ConfigCompiler::CompileFile(configPath); - Dictionary::Ptr context = new Dictionary(); - expression->Evaluate(context); + ExecuteExpression(expression); + delete expression; } } else if (!vm.count("no-config")) { Expression *expression = ConfigCompiler::CompileFile(Application::GetSysconfDir() + "/icinga2/icinga2.conf"); - Dictionary::Ptr context = new Dictionary(); - expression->Evaluate(context); + ExecuteExpression(expression); + delete expression; } /* Load cluster config files - this should probably be in libremote but @@ -115,8 +129,8 @@ static bool LoadConfigFiles(const boost::program_options::variables_map& vm, con String name, fragment; BOOST_FOREACH(boost::tie(name, fragment), ConfigFragmentRegistry::GetInstance()->GetItems()) { Expression *expression = ConfigCompiler::CompileText(name, fragment); - Dictionary::Ptr context = new Dictionary(); - expression->Evaluate(context); + ExecuteExpression(expression); + delete expression; } ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(); diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index e3cbc3db3..7bcb627b4 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -46,7 +46,7 @@ Value Expression::Evaluate(const Object::Ptr& context, DebugHint *dhint) const return DoEvaluate(context, dhint); } catch (const std::exception& ex) { - if (boost::get_error_info(ex)) + if (dynamic_cast(&ex) || boost::get_error_info(ex)) throw; else BOOST_THROW_EXCEPTION(ConfigError("Error while evaluating expression: " + String(ex.what())) -- 2.40.0