From 358aa3a6a5b054fd2bf47ea599c3674eb40a76dc Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 12 Jul 2013 18:25:18 +0200 Subject: [PATCH] livestatus: use enum for error codes, fix empty line refs #4372 --- components/livestatus/query.cpp | 25 ++++++++++++++++--------- components/livestatus/query.h | 7 +++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/components/livestatus/query.cpp b/components/livestatus/query.cpp index 74513257e..b7772fd13 100644 --- a/components/livestatus/query.cpp +++ b/components/livestatus/query.cpp @@ -47,6 +47,13 @@ using namespace livestatus; Query::Query(const std::vector& lines) : m_KeepAlive(false), m_OutputFormat("csv"), m_ColumnHeaders(true), m_Limit(-1) { + if (lines.size() == 0) { + m_Verb = "ERROR"; + m_ErrorCode = LivestatusErrorQuery; + m_ErrorMessage = "Empty Query. Aborting."; + return; + } + String line = lines[0]; size_t sp_index = line.FindFirstOf(" "); @@ -65,7 +72,7 @@ Query::Query(const std::vector& lines) m_Table = target; } else { m_Verb = "ERROR"; - m_ErrorCode = 452; + m_ErrorCode = LivestatusErrorQuery; m_ErrorMessage = "Unknown livestatus verb: " + m_Verb; return; } @@ -96,7 +103,7 @@ Query::Query(const std::vector& lines) if (!filter) { m_Verb = "ERROR"; - m_ErrorCode = 452; + m_ErrorCode = LivestatusErrorQuery; m_ErrorMessage = "Invalid filter specification: " + line; return; } @@ -108,7 +115,7 @@ Query::Query(const std::vector& lines) if (tokens.size() < 2) { m_Verb = "ERROR"; - m_ErrorCode = 452; + m_ErrorCode = LivestatusErrorQuery; m_ErrorMessage = "Missing aggregator column name: " + line; return; } @@ -138,7 +145,7 @@ Query::Query(const std::vector& lines) if (!filter) { m_Verb = "ERROR"; - m_ErrorCode = 452; + m_ErrorCode = LivestatusErrorQuery; m_ErrorMessage = "Invalid filter specification: " + line; return; } @@ -312,7 +319,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream) Table::Ptr table = Table::GetByName(m_Table); if (!table) { - SendResponse(stream, 404, "Table '" + m_Table + "' does not exist."); + SendResponse(stream, LivestatusErrorNotFound, "Table '" + m_Table + "' does not exist."); return; } @@ -364,14 +371,14 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream) std::ostringstream result; PrintResultSet(result, columns, rs); - SendResponse(stream, 200, result.str()); + SendResponse(stream, LivestatusErrorOK, result.str()); } void Query::ExecuteCommandHelper(const Stream::Ptr& stream) { Log(LogInformation, "livestatus", "Executing command: " + m_Command); ExternalCommandProcessor::Execute(m_Command); - SendResponse(stream, 200, ""); + SendResponse(stream, LivestatusErrorOK, ""); } void Query::ExecuteErrorHelper(const Stream::Ptr& stream) @@ -384,7 +391,7 @@ void Query::SendResponse(const Stream::Ptr& stream, int code, const String& data if (m_ResponseHeader == "fixed16") PrintFixed16(stream, code, data); - if (m_ResponseHeader == "fixed16" || code == 200) + if (m_ResponseHeader == "fixed16" || code == LivestatusErrorOK) stream->Write(data.CStr(), data.GetLength()); } @@ -417,7 +424,7 @@ bool Query::Execute(const Stream::Ptr& stream) std::ostringstream info; st->Print(info); Log(LogWarning, "livestatus", info.str()); - SendResponse(stream, 452, boost::diagnostic_information(ex)); + SendResponse(stream, LivestatusErrorQuery, boost::diagnostic_information(ex)); } if (!m_KeepAlive) { diff --git a/components/livestatus/query.h b/components/livestatus/query.h index 16ce7efe7..843e47c25 100644 --- a/components/livestatus/query.h +++ b/components/livestatus/query.h @@ -32,6 +32,13 @@ using namespace icinga; namespace livestatus { +enum LivestatusError +{ + LivestatusErrorOK = 200, + LivestatusErrorNotFound = 404, + LivestatusErrorQuery = 452 +}; + /** * @ingroup livestatus */ -- 2.40.0