From: Alexander A. Klimov Date: Thu, 14 Feb 2019 15:07:06 +0000 (+0100) Subject: Add HttpUtility::SendJsonError() overload for Boost/Beast X-Git-Tag: v2.11.0-rc1~174^2~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04a9879acc0cf33538058725f71a295fd7d43ef5;p=icinga2 Add HttpUtility::SendJsonError() overload for Boost/Beast --- diff --git a/lib/remote/httputility.cpp b/lib/remote/httputility.cpp index f7fef7713..fa54d9c89 100644 --- a/lib/remote/httputility.cpp +++ b/lib/remote/httputility.cpp @@ -103,6 +103,24 @@ void HttpUtility::SendJsonError(HttpResponse& response, const Dictionary::Ptr& p HttpUtility::SendJsonBody(response, params, result); } +void HttpUtility::SendJsonError(boost::beast::http::response& response, + const Dictionary::Ptr& params, int code, const String& info, const String& diagnosticInformation) +{ + Dictionary::Ptr result = new Dictionary({ { "error", code } }); + + if (!info.IsEmpty()) { + result->Set("status", info); + } + + if (params && HttpUtility::GetLastParameter(params, "verbose") && !diagnosticInformation.IsEmpty()) { + result->Set("diagnostic_information", diagnosticInformation); + } + + response.result(code); + + HttpUtility::SendJsonBody(response, params, result); +} + String HttpUtility::GetErrorNameByCode(const int code) { switch(code) { diff --git a/lib/remote/httputility.hpp b/lib/remote/httputility.hpp index e758b2663..7122e3e71 100644 --- a/lib/remote/httputility.hpp +++ b/lib/remote/httputility.hpp @@ -26,6 +26,8 @@ public: static Value GetLastParameter(const Dictionary::Ptr& params, const String& key); static void SendJsonError(HttpResponse& response, const Dictionary::Ptr& params, const int code, const String& verbose = String(), const String& diagnosticInformation = String()); + static void SendJsonError(boost::beast::http::response& response, const Dictionary::Ptr& params, const int code, + const String& verbose = String(), const String& diagnosticInformation = String()); private: static String GetErrorNameByCode(int code);