From: Gunnar Beutner Date: Mon, 9 Dec 2013 08:52:09 +0000 (+0100) Subject: Improve error messages for the check result reader. X-Git-Tag: v0.0.6~74 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=203cf73bf78bdc0a98f51a1f7ab1a599531b7c00;p=icinga2 Improve error messages for the check result reader. Fixes #5275 --- diff --git a/components/compat/checkresultreader.cpp b/components/compat/checkresultreader.cpp index 8071c679e..4394512cd 100644 --- a/components/compat/checkresultreader.cpp +++ b/components/compat/checkresultreader.cpp @@ -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 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()); } } - diff --git a/lib/base/convert.h b/lib/base/convert.h index 559b952e8..67cb26191 100644 --- a/lib/base/convert.h +++ b/lib/base/convert.h @@ -38,13 +38,25 @@ public: template static long ToLong(const T& val) { - return boost::lexical_cast(val); + try { + return boost::lexical_cast(val); + } catch (std::exception&) { + std::ostringstream msgbuf; + msgbuf << "Can't convert '" << val << "' to an integer."; + BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str())); + } } template static double ToDouble(const T& val) { - return boost::lexical_cast(val); + try { + return boost::lexical_cast(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);