Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
mkbouncer: return reason for failure (#5)
Browse files Browse the repository at this point in the history
Bump the versioned namespace since we'll tag v0.3.0 since this
change breaks the previous ABI.

Closes #4.
  • Loading branch information
bassosimone authored Jan 2, 2020
1 parent 9533afa commit 5967a66
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.vscode
/build
26 changes: 25 additions & 1 deletion mkbouncer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// public symbols exported by this library are enclosed.
///
/// See <https://github.com/measurement-kit/measurement-kit/issues/1867#issuecomment-514562622>.
#define MKBOUNCER_INLINE_NAMESPACE v0_1_1_or_greater
#define MKBOUNCER_INLINE_NAMESPACE v0_3_0_or_greater

namespace mk {
namespace bouncer {
Expand Down Expand Up @@ -69,6 +69,9 @@ class Response {
/// good indicates whether we good a good response.
bool good = false;

/// reason contains the reason for failure.
std::string reason;

/// collectors lists all available collectors.
std::vector<Record> collectors;

Expand All @@ -91,6 +94,8 @@ Response perform(const Request &request) noexcept;

#include <sstream>

#include <curl/curl.h>

#include "json.hpp"
#include "mkcurl.hpp"
#include "mkmock.hpp"
Expand All @@ -105,6 +110,22 @@ namespace mk {
namespace bouncer {
inline namespace MKBOUNCER_INLINE_NAMESPACE {

// curl_reason_for_failure contains the cURL reason for failure.
static std::string curl_reason_for_failure(
const curl::Response &response) noexcept {
if (response.error != 0) {
std::string rv = "bouncer: ";
rv += curl_easy_strerror((CURLcode)response.error);
return rv;
}
if (response.status_code != 200) {
std::string rv = "bouncer: ";
rv += curl_easy_strerror(CURLE_HTTP_RETURNED_ERROR);
return rv;
}
return "bouncer: unknown libcurl error";
}

// log_body is a helper to log about a body.
static void log_body(const std::string &prefix, const std::string &body,
std::vector<std::string> &logs) noexcept {
Expand Down Expand Up @@ -137,6 +158,7 @@ Response perform(const Request &request) noexcept {
body = doc.dump();
} catch (const std::exception &exc) {
response.logs.push_back(exc.what());
response.reason = exc.what();
return response;
}
log_body("Request", body, response.logs);
Expand All @@ -149,6 +171,7 @@ Response perform(const Request &request) noexcept {
MKBOUNCER_HOOK(curl_response_error, curl_response.error);
MKBOUNCER_HOOK(curl_response_status_code, curl_response.status_code);
if (curl_response.error != 0 || curl_response.status_code != 200) {
response.reason = curl_reason_for_failure(curl_response);
return response;
}
MKBOUNCER_HOOK(curl_response_body, curl_response.body);
Expand Down Expand Up @@ -196,6 +219,7 @@ Response perform(const Request &request) noexcept {
}
} catch (const std::exception &exc) {
response.logs.push_back(exc.what());
response.reason = exc.what();
return response;
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ const uint8_t binary_input[] = {
};
// clang-format on

TEST_CASE("curl_reason_for_failure works") {
SECTION("With a real curl error") {
mk::curl::Response response;
response.error = CURLE_AGAIN;
std::string v = mk::bouncer::curl_reason_for_failure(response);
REQUIRE(v == "bouncer: Socket not ready for send/recv");
}
SECTION("With an HTTP error") {
mk::curl::Response response;
response.status_code = 404;
std::string v = mk::bouncer::curl_reason_for_failure(response);
REQUIRE(v == "bouncer: HTTP response code said error");
}
SECTION("With an unexpected error") {
mk::curl::Response response;
response.status_code = 200;
std::string v = mk::bouncer::curl_reason_for_failure(response);
REQUIRE(v == "bouncer: unknown libcurl error");
}
}

TEST_CASE("We deal with perform errors") {
SECTION("On failure to serialize the request body") {
mk::bouncer::Request request;
Expand Down

0 comments on commit 5967a66

Please sign in to comment.