]> granicus.if.org Git - icinga2/commitdiff
livestatus: use enum for error codes, fix empty line
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 12 Jul 2013 16:25:18 +0000 (18:25 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 12 Jul 2013 16:25:18 +0000 (18:25 +0200)
refs #4372

components/livestatus/query.cpp
components/livestatus/query.h

index 74513257e4aadcc5203addc8fbe14a1ec6ceea0f..b7772fd13adcb56700025bd1851e8222e0479384 100644 (file)
@@ -47,6 +47,13 @@ using namespace livestatus;
 Query::Query(const std::vector<String>& 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<String>& 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<String>& 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<String>& 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<String>& 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) {
index 16ce7efe7d9d9aa3c43643c822b7d074c87680e2..843e47c2554abce7191483a2b6af28b1b118c35f 100644 (file)
@@ -32,6 +32,13 @@ using namespace icinga;
 namespace livestatus
 {
 
+enum LivestatusError
+{
+       LivestatusErrorOK = 200,
+       LivestatusErrorNotFound = 404,
+       LivestatusErrorQuery = 452
+};
+
 /**
  * @ingroup livestatus
  */