Skip to content

Commit

Permalink
Returning status 500 if there is an error. Simplified other things
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen committed Oct 9, 2024
1 parent 50596e1 commit 52c8ef7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
46 changes: 21 additions & 25 deletions collector/lib/CollectorConfigInspector.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "CollectorConfigInspector.h"

#include <string>

#include <Logging.h>
#include <string>

#include <google/protobuf/util/json_util.h>

Expand All @@ -13,32 +12,24 @@ const std::string CollectorConfigInspector::kBaseRoute = "/state/config";
CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr<CollectorConfig> 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) {
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions collector/lib/CollectorConfigInspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class CollectorConfigInspector : public IntrospectionEndpoint {

private:
const std::shared_ptr<CollectorConfig> config_;
Json::FastWriter writer_;
Json::Value configToJson();
std::string configToJson(bool& isError);
};

} // namespace collector
Expand Down

0 comments on commit 52c8ef7

Please sign in to comment.