diff --git a/collector/lib/CollectorConfigInspector.cpp b/collector/lib/CollectorConfigInspector.cpp index 6478aef097..1f8d16a9a3 100644 --- a/collector/lib/CollectorConfigInspector.cpp +++ b/collector/lib/CollectorConfigInspector.cpp @@ -1,8 +1,7 @@ #include "CollectorConfigInspector.h" -#include - #include +#include #include @@ -13,32 +12,24 @@ const std::string CollectorConfigInspector::kBaseRoute = "/state/config"; CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr config) : config_(config) { } -Json::Value CollectorConfigInspector::configToJson() { - Json::Value root; +std::string CollectorConfigInspector::configToJson(bool& isError) { const auto& runtime_config = config_->GetRuntimeConfig(); if (!runtime_config.has_value()) { - return root; + return "{}"; } - std::string jsonString; - const auto& config = runtime_config.value(); - google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(config, &jsonString); - - if (!status.ok()) { - CLOG(WARNING) << "Failed to convert protobuf object to JSON: " << status.ToString(); - return Json::Value(); - } - - Json::CharReaderBuilder readerBuilder; - std::string errs; - std::istringstream iss(jsonString); - if (!Json::parseFromStream(readerBuilder, iss, &root, &errs)) { - CLOG(ERROR) << "Failed to parse JSON string: " << errs; - return Json::Value(); - } + + std::string jsonString; + const auto& config = runtime_config.value(); + google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(config, &jsonString); + + if (!status.ok()) { + isError = true; + CLOG(WARNING) << "Failed to convert protobuf object to JSON: " << status.ToString(); + return "{\"error\": \"Failed to convert protobuf object to JSON\""; } - return root; + return jsonString; } bool CollectorConfigInspector::handleGet(CivetServer* server, struct mg_connection* conn) { @@ -48,10 +39,15 @@ bool CollectorConfigInspector::handleGet(CivetServer* server, struct mg_connecti return ServerError(conn, "unable to read request"); } - Json::Value root = configToJson(); + bool isError = false; + std::string jsonString = configToJson(isError); - mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "%s\r\n", writer_.write(root).c_str()); + if (isError) { + mg_printf(conn, "HTTP/1.1 500 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); + } else { + mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); + } + mg_printf(conn, "%s\r\n", jsonString.c_str()); return true; } diff --git a/collector/lib/CollectorConfigInspector.h b/collector/lib/CollectorConfigInspector.h index 1104b76aa9..cda577f4ae 100644 --- a/collector/lib/CollectorConfigInspector.h +++ b/collector/lib/CollectorConfigInspector.h @@ -21,8 +21,7 @@ class CollectorConfigInspector : public IntrospectionEndpoint { private: const std::shared_ptr config_; - Json::FastWriter writer_; - Json::Value configToJson(); + std::string configToJson(bool& isError); }; } // namespace collector