Skip to content

Commit

Permalink
Improve logs and error handling
Browse files Browse the repository at this point in the history
Refs: #52
  • Loading branch information
orontee committed Oct 5, 2023
1 parent 27181c9 commit a683551
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "errors.h"
#include "logging.h"
#include <sstream>

Json::Value taranis::HttpClient::get(const std::string &url) {
BOOST_LOG_TRIVIAL(debug) << "Sending GET request " << url;
Expand Down Expand Up @@ -29,12 +30,20 @@ Json::Value taranis::HttpClient::get(const std::string &url) {
throw HttpError{response_code};
}

BOOST_LOG_TRIVIAL(debug) << "Received " << response_data.size() << " bytes";

Json::Value root;
Json::Reader reader;
if (not reader.parse(response_data, root)) {
BOOST_LOG_TRIVIAL(error)
<< "JSON parser error " << response_data << " " << url;
throw JsonParseError{};
Json::CharReaderBuilder reader;
reader["collectComments"] = false;
std::string json_errors;
std::stringstream input_stream{response_data};
try {
if (not Json::parseFromStream(reader, input_stream, &root, &json_errors)) {
BOOST_LOG_TRIVIAL(error)
<< "JSON parser error " << json_errors << " " << url;
}
} catch (const Json::Exception& error) {
BOOST_LOG_TRIVIAL(error) << error.what();
}
return root;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ int event_handler(int event_type, int param_one, int param_two) {
} catch (const std::logic_error &error) {
BOOST_LOG_TRIVIAL(error) << "Unhandled logic error " << error.what();
throw;
} catch (const std::exception &error) {
BOOST_LOG_TRIVIAL(error) << "Unhandled error " << error.what();
throw;
}
}

Expand Down

0 comments on commit a683551

Please sign in to comment.