]> granicus.if.org Git - icinga2/commitdiff
Improve error messages for the check result reader.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Dec 2013 08:52:09 +0000 (09:52 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Dec 2013 08:52:09 +0000 (09:52 +0100)
Fixes #5275

components/compat/checkresultreader.cpp
lib/base/convert.h

index 8071c679e01145cf4b1c99725b81c5605ad900de..4394512cd15e79a30fdc85a3fa516ffd33220b7d 100644 (file)
@@ -26,6 +26,8 @@
 #include "base/convert.h"
 #include "base/application.h"
 #include "base/utility.h"
+#include "base/exception.h"
+#include "base/context.h"
 #include <fstream>
 
 using namespace icinga;
@@ -48,11 +50,15 @@ void CheckResultReader::Start(void)
  */
 void CheckResultReader::ReadTimerHandler(void) const
 {
+       CONTEXT("Processing check result files in '" + GetSpoolDir() + "'");
+
        Utility::Glob(GetSpoolDir() + "/c??????.ok", boost::bind(&CheckResultReader::ProcessCheckResultFile, this, _1));
 }
 
 void CheckResultReader::ProcessCheckResultFile(const String& path) const
 {
+       CONTEXT("Processing check result file '" + path + "'");
+
        String crfile = String(path.Begin(), path.End() - 3); /* Remove the ".ok" extension. */
 
        std::ifstream fp;
@@ -80,8 +86,17 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
        }
 
        /* Remove the checkresult files. */
-       (void)unlink(path.CStr());
-       (void)unlink(crfile.CStr());
+       if (unlink(path.CStr()) < 0)
+               BOOST_THROW_EXCEPTION(posix_error()
+                   << boost::errinfo_api_function("unlink")
+                   << boost::errinfo_errno(errno)
+                   << boost::errinfo_file_name(path));
+
+       if (unlink(crfile.CStr()) < 0)
+               BOOST_THROW_EXCEPTION(posix_error()
+                   << boost::errinfo_api_function("unlink")
+                   << boost::errinfo_errno(errno)
+                   << boost::errinfo_file_name(crfile));
 
        Host::Ptr host = Host::GetByName(attrs["host_name"]);
 
@@ -120,4 +135,3 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
                service->SetNextCheck(Utility::GetTime() + service->GetCheckInterval());
        }
 }
-
index 559b952e8ea66d7b64997f0ed2bac3887a921427..67cb26191792b2404be68e468bd9a7092acae517 100644 (file)
@@ -38,13 +38,25 @@ public:
        template<typename T>
        static long ToLong(const T& val)
        {
-               return boost::lexical_cast<long>(val);
+               try {
+                       return boost::lexical_cast<long>(val);
+               } catch (std::exception&) {
+                       std::ostringstream msgbuf;
+                       msgbuf << "Can't convert '" << val << "' to an integer.";
+                       BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
+               }
        }
 
        template<typename T>
        static double ToDouble(const T& val)
        {
-               return boost::lexical_cast<double>(val);
+               try {
+                       return boost::lexical_cast<double>(val);
+               } catch (std::exception&) {
+                       std::ostringstream msgbuf;
+                       msgbuf << "Can't convert '" << val << "' to a floating point number.";
+                       BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
+               }
        }
 
        static bool ToBool(const String& val);