From 0685889e45977086f2ecd968c85b52b0bb4bc81c Mon Sep 17 00:00:00 2001 From: Will Gittoes Date: Fri, 15 Feb 2019 14:52:05 +1100 Subject: [PATCH] Ennicen the error message --- src/encoder.cpp | 15 +++++++++++++-- test/agent_writer_test.cpp | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/encoder.cpp b/src/encoder.cpp index 42626b61..c80fce15 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -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 sampler) @@ -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; } } diff --git a/test/agent_writer_test.cpp b/test/agent_writer_test.cpp index 59d54225..6e7993cc 100644 --- a/test/agent_writer_test.cpp +++ b/test/agent_writer_test.cpp @@ -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( + {{"// 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 == ""); }