]> granicus.if.org Git - icinga2/commitdiff
Improve error messages for the config parser
authorGunnar Beutner <gunnar@beutner.name>
Fri, 21 Nov 2014 08:07:08 +0000 (09:07 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 21 Nov 2014 08:07:08 +0000 (09:07 +0100)
refs #7699

lib/cli/daemoncommand.cpp
lib/config/expression.cpp

index 966af8568cdbc8cc539919890a41807e749aa092..5f1ac05132e8233c2c1fa8e3673ff5e74e3875f1 100644 (file)
@@ -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<errinfo_debuginfo>(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<Expression *> 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<std::vector<std::string> >()) {
                        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();
index e3cbc3db34c8b39304379d372c0c75807a4d510d..7bcb627b48aeb3bb84daf296c504d01718eb7606 100644 (file)
@@ -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<boost::errinfo_nested_exception>(ex))
+               if (dynamic_cast<const ConfigError *>(&ex) || boost::get_error_info<boost::errinfo_nested_exception>(ex))
                        throw;
                else
                        BOOST_THROW_EXCEPTION(ConfigError("Error while evaluating expression: " + String(ex.what()))