From 82a1897c2df0b6477a0dcdaba87a1fef87c2a064 Mon Sep 17 00:00:00 2001 From: David Yastremsky Date: Wed, 26 Feb 2025 11:28:32 -0800 Subject: [PATCH 1/3] Provide preview of JSON when parse fails Clean up formatting --- include/triton/common/triton_json.h | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/triton/common/triton_json.h b/include/triton/common/triton_json.h index cded06e..c3c0896 100644 --- a/include/triton/common/triton_json.h +++ b/include/triton/common/triton_json.h @@ -184,6 +184,11 @@ class TritonJson { std::string(GetParseError_En(document_.GetParseError())) + " at " + std::to_string(document_.GetErrorOffset()))); } + TRITONJSON_STATUSTYPE status = ParseErrorHandler(document_, std::string(base, size)); + if (status != TRITONJSON_STATUSSUCCESS) { + return status; + } + allocator_ = &document_.GetAllocator(); return TRITONJSON_STATUSSUCCESS; } @@ -194,6 +199,31 @@ class TritonJson { return Parse(json.data(), json.size()); } + // Helper function for Parse(const char* base, const size_t size) to handle + // errors. Return error message if parsing failed. + TRITONJSON_STATUSTYPE ParseErrorHandler( + const rapidjson::Document& document, const std::string& json) + { + if (document.HasParseError()) { + std::ostringstream error_stream; + error_stream << "failed to parse the request JSON buffer: " + << GetParseError_En(document.GetParseError()) + << " at offset " << document.GetErrorOffset() << "."; + + // Show part of the JSON to help debugging + const size_t preview_length = 100; + std::string json_preview = json.substr(0, preview_length); + if (json.size() > preview_length) { + json_preview += "..."; + } + + error_stream << " JSON Preview: \"" << json_preview << "\""; + + return TRITONJSON_STATUSRETURN(error_stream.str()); + } + return TRITONJSON_STATUSSUCCESS; + } + // Write JSON representation into a 'buffer' in a compact // format. Can only be called for a top-level document value, // otherwise error is returned. From c134020b9254e19ee0431d453618ff6265e005f1 Mon Sep 17 00:00:00 2001 From: David Yastremsky Date: Wed, 26 Feb 2025 11:42:49 -0800 Subject: [PATCH 2/3] Clean up formatting Clean up formatting --- include/triton/common/triton_json.h | 41 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/include/triton/common/triton_json.h b/include/triton/common/triton_json.h index c3c0896..d55a01a 100644 --- a/include/triton/common/triton_json.h +++ b/include/triton/common/triton_json.h @@ -184,9 +184,10 @@ class TritonJson { std::string(GetParseError_En(document_.GetParseError())) + " at " + std::to_string(document_.GetErrorOffset()))); } - TRITONJSON_STATUSTYPE status = ParseErrorHandler(document_, std::string(base, size)); + TRITONJSON_STATUSTYPE status = + ParseErrorHandler(document_, std::string(base, size)); if (status != TRITONJSON_STATUSSUCCESS) { - return status; + return status; } allocator_ = &document_.GetAllocator(); @@ -202,26 +203,26 @@ class TritonJson { // Helper function for Parse(const char* base, const size_t size) to handle // errors. Return error message if parsing failed. TRITONJSON_STATUSTYPE ParseErrorHandler( - const rapidjson::Document& document, const std::string& json) - { - if (document.HasParseError()) { - std::ostringstream error_stream; - error_stream << "failed to parse the request JSON buffer: " - << GetParseError_En(document.GetParseError()) - << " at offset " << document.GetErrorOffset() << "."; - - // Show part of the JSON to help debugging - const size_t preview_length = 100; - std::string json_preview = json.substr(0, preview_length); - if (json.size() > preview_length) { - json_preview += "..."; - } + const rapidjson::Document& document, const std::string& json) + { + if (document.HasParseError()) { + std::ostringstream error_stream; + error_stream << "failed to parse the request JSON buffer: " + << GetParseError_En(document.GetParseError()) + << " at offset " << document.GetErrorOffset() << "."; + + // Show part of the JSON to help debugging + const size_t preview_length = 100; + std::string json_preview = json.substr(0, preview_length); + if (json.size() > preview_length) { + json_preview += "..."; + } - error_stream << " JSON Preview: \"" << json_preview << "\""; + error_stream << " JSON Preview: \"" << json_preview << "\""; - return TRITONJSON_STATUSRETURN(error_stream.str()); - } - return TRITONJSON_STATUSSUCCESS; + return TRITONJSON_STATUSRETURN(error_stream.str()); + } + return TRITONJSON_STATUSSUCCESS; } // Write JSON representation into a 'buffer' in a compact From 6191cd84ace5937eceae7ac04ba4195fb4c67c44 Mon Sep 17 00:00:00 2001 From: David Yastremsky Date: Wed, 26 Feb 2025 13:29:03 -0800 Subject: [PATCH 3/3] Print entire JSON --- include/triton/common/triton_json.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/triton/common/triton_json.h b/include/triton/common/triton_json.h index d55a01a..75750cb 100644 --- a/include/triton/common/triton_json.h +++ b/include/triton/common/triton_json.h @@ -213,12 +213,9 @@ class TritonJson { // Show part of the JSON to help debugging const size_t preview_length = 100; - std::string json_preview = json.substr(0, preview_length); - if (json.size() > preview_length) { - json_preview += "..."; - } + std::string json_text = json.substr(0); - error_stream << " JSON Preview: \"" << json_preview << "\""; + error_stream << " JSON: \"" << json_text << "\""; return TRITONJSON_STATUSRETURN(error_stream.str()); }