-
Notifications
You must be signed in to change notification settings - Fork 11
Handle error response codes to HTTP requests. #61
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,8 @@ class MetadataReporter { | |
|
||
// Send the given set of metadata. | ||
void SendMetadata( | ||
std::map<MonitoredResource, MetadataAgent::Metadata>&& metadata); | ||
std::map<MonitoredResource, MetadataAgent::Metadata>&& metadata) | ||
throw (boost::system::system_error); | ||
|
||
const MetadataAgent& agent_; | ||
Environment environment_; | ||
|
@@ -171,9 +172,13 @@ void MetadataReporter::ReportMetadata() { | |
if (agent_.config_.VerboseLogging()) { | ||
LOG(INFO) << "Sending metadata request to server"; | ||
} | ||
SendMetadata(agent_.GetMetadataMap()); | ||
if (agent_.config_.VerboseLogging()) { | ||
LOG(INFO) << "Metadata request sent successfully"; | ||
try { | ||
SendMetadata(agent_.GetMetadataMap()); | ||
if (agent_.config_.VerboseLogging()) { | ||
LOG(INFO) << "Metadata request sent successfully"; | ||
} | ||
} catch (const boost::system::system_error& e) { | ||
LOG(ERROR) << "Unsuccessful: " << e.what(); | ||
} | ||
std::this_thread::sleep_for(period_); | ||
} | ||
|
@@ -185,7 +190,8 @@ namespace { | |
void SendMetadataRequest(std::vector<json::value>&& entries, | ||
const std::string& endpoint, | ||
const std::string& auth_header, | ||
bool verbose_logging) { | ||
bool verbose_logging) | ||
throw (boost::system::system_error) { | ||
json::value update_metadata_request = json::object({ | ||
{"entries", json::array(std::move(entries))}, | ||
}); | ||
|
@@ -204,6 +210,13 @@ void SendMetadataRequest(std::vector<json::value>&& entries, | |
request << boost::network::header("Authorization", auth_header); | ||
request << boost::network::body(request_body); | ||
http::client::response response = client.post(request); | ||
if (status(response) != 200) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would accept anything in the 2xx range, technically a POST can result in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
throw boost::system::system_error( | ||
boost::system::errc::make_error_code(boost::system::errc::not_connected), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe not_connected is the best exception to throw here, but if it's just a matter of getting "something" to throw, it's fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that code is never examined. |
||
format::Substitute("Server responded with '{{message}}' ({{code}})", | ||
{{"message", status_message(response)}, | ||
{"code", format::str(status(response))}})); | ||
} | ||
if (verbose_logging) { | ||
LOG(INFO) << "Server responded with " << body(response); | ||
} | ||
|
@@ -213,7 +226,8 @@ void SendMetadataRequest(std::vector<json::value>&& entries, | |
} | ||
|
||
void MetadataReporter::SendMetadata( | ||
std::map<MonitoredResource, MetadataAgent::Metadata>&& metadata) { | ||
std::map<MonitoredResource, MetadataAgent::Metadata>&& metadata) | ||
throw (boost::system::system_error) { | ||
if (metadata.empty()) { | ||
if (agent_.config_.VerboseLogging()) { | ||
LOG(INFO) << "No data to send"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
#include <boost/network/protocol/http/client.hpp> | ||
#include <chrono> | ||
|
||
#include "format.h" | ||
#include "instance.h" | ||
#include "json.h" | ||
#include "logging.h" | ||
|
@@ -169,6 +170,13 @@ json::value DockerReader::QueryDocker(const std::string& path) const | |
} | ||
try { | ||
http::local_client::response response = client.get(request); | ||
if (status(response) != 200) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as a note, I'm fine with just a 200 here as you have it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed for consistency. |
||
throw boost::system::system_error( | ||
boost::system::errc::make_error_code(boost::system::errc::not_connected), | ||
format::Substitute("Server responded with '{{message}}' ({{code}})", | ||
{{"message", status_message(response)}, | ||
{"code", format::str(status(response))}})); | ||
} | ||
#ifdef VERBOSE | ||
LOG(DEBUG) << "QueryDocker: Response: " << body(response); | ||
#endif | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,19 @@ | |
|
||
namespace format { | ||
|
||
namespace { | ||
template<class T> | ||
std::string AsString(T v) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this being used anywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 32-34 below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, my eyes certainly failed me 🤣 |
||
std::ostringstream out; | ||
out << v; | ||
return out.str(); | ||
} | ||
} | ||
|
||
std::string str(int i) { return AsString(i); } | ||
std::string str(double d) { return AsString(d); } | ||
std::string str(bool b) { return AsString(b); } | ||
|
||
std::string Substitute(const std::string& format, | ||
const std::map<std::string, std::string>&& params) | ||
throw(Exception) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Unsuccessful/Metadata request unsuccessful/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.