Skip to content

Commit

Permalink
Ennicen the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
willgittoes-dd committed Feb 15, 2019
1 parent 2b74f7e commit 0685889
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const std::string header_dd_meta_lang = "Datadog-Meta-Lang";
const std::string header_dd_meta_lang_version = "Datadog-Meta-Lang-Version";
const std::string header_dd_meta_tracer_version = "Datadog-Meta-Tracer-Version";
const std::string header_dd_trace_count = "X-Datadog-Trace-Count";

const size_t RESPONSE_ERROR_REGION_SIZE = 50;
} // namespace

AgentHttpEncoder::AgentHttpEncoder(std::shared_ptr<SampleProvider> sampler)
Expand Down Expand Up @@ -59,8 +61,17 @@ void AgentHttpEncoder::handleResponse(const std::string& response) {
return; // No priority sampling info.
}
sampler_->configure(config[priority_sampling_key]);
} catch (const json::parse_error&) {
std::cerr << "Unable to parse response from agent. Response was: " << response << std::endl;
} catch (const json::parse_error& error) {
size_t start = (error.byte > (RESPONSE_ERROR_REGION_SIZE / 2))
? error.byte - (RESPONSE_ERROR_REGION_SIZE / 2)
: 0;
size_t size = std::min(response.length() - start, RESPONSE_ERROR_REGION_SIZE);
std::string prefix = (start > 0) ? "..." : "";
std::string suffix = ((start + size) < response.length()) ? "..." : "";
std::string response_region = response.substr(start, size);
std::cerr << "Unable to parse response from agent." << std::endl
<< "Error was: " << error.what() << std::endl
<< "Error near: " << prefix << response_region << suffix << std::endl;
return;
}
}
Expand Down
37 changes: 32 additions & 5 deletions test/agent_writer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,44 @@ TEST_CASE("writer") {
}

SECTION("handle dodgy responses") {
handle->response =
"// What?! This isn't JSON! Everyone knows real JSON doesn't have comments...";
struct BadResponseTest {
std::string response;
std::string error;
};

auto bad_response_test_case = GENERATE(values<BadResponseTest>(
{{"// Error at start, short body",
("Unable to parse response from agent.\n"
"Error was: [json.exception.parse_error.101] parse error at 1: syntax error - "
"invalid literal; last read: '/'\n"
"Error near: // Error at start, short body\n")},
{"{\"lol\" // Error near start, error message should have truncated "
"body. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9",
"Unable to parse response from agent.\n"
"Error was: [json.exception.parse_error.101] parse error at 8: syntax error - invalid "
"literal; last read: '\"lol\" /'; expected ':'\n"
"Error near: {\"lol\" // Error near start, error message should h...\n"},
{"{\"Error near the end, should be truncated. 0 1 2 3 4 5 6 7 8 9 \", oh noes",
"Unable to parse response from agent.\n"
"Error was: [json.exception.parse_error.101] parse error at 65: syntax error - "
"unexpected ','; expected ':'\n"
"Error near: ...d. 0 1 2 3 4 5 6 7 8 9 \", oh noes\n"},
{"{\"Error in the middle, truncated from both ends\" lol 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 "
"6 7 8 9",
"Unable to parse response from agent.\n"
"Error was: [json.exception.parse_error.101] parse error at 50: syntax error - invalid "
"literal; last read: '\"Error in the middle, truncated from both ends\" l'; expected "
"':'\n"
"Error near: ...uncated from both ends\" lol 0 1 2 3 4 5 6 7 8 9 0 ...\n"}}));

handle->response = bad_response_test_case.response;
writer.write(make_trace(
{TestSpanData{"web", "service", "resource", "service.name", 1, 1, 0, 69, 420, 0}}));

std::stringstream error_message;
std::streambuf* stderr = std::cerr.rdbuf(error_message.rdbuf());
writer.flush(std::chrono::seconds(10));
REQUIRE(error_message.str() ==
("Unable to parse response from agent. Response was: "
"// What?! This isn't JSON! Everyone knows real JSON doesn't have comments...\n"));
REQUIRE(error_message.str() == bad_response_test_case.error);
std::cerr.rdbuf(stderr); // Restore stderr.
REQUIRE(sampler->config == "");
}
Expand Down

0 comments on commit 0685889

Please sign in to comment.