-
Notifications
You must be signed in to change notification settings - Fork 74
feat: Provide preview of JSON when Parse fails #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -184,6 +184,12 @@ 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 +200,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 += "..."; | ||
} | ||
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. Any strong reason to not just include the whole thing? For example, can the error start at some point after the preview length?
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 wanted to avoid that in case the JSON might be very long and crashes the program. I can increase the preview length to the limit of your choice (maybe 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. That is a fair point that it could very well be an issue after the 100th character. A large limit could resolve that while balancing the risk of the JSON being too long and causing a crash. |
||
|
||
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. | ||
|
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.
Can you add a before/after example in the PR description to highlight the new error/UX improvement?
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.
Sure! Let me run it through CI then. I wanted your feedback first in case there was a reason not to do this.