feat: Provide preview of JSON when Parse fails#131
Conversation
Clean up formatting
35b1acf to
82a1897
Compare
Clean up formatting
d8177bd to
c134020
Compare
| // 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.
Any strong reason to not just include the whole thing?
For example, can the error start at some point after the preview length?
{... /* 100+ chars */, "my_invalid_json".123}
There was a problem hiding this comment.
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 std::numeric_limits<std::size_t>::max()or print the whole thing. Any preference?
There was a problem hiding this comment.
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.
| std::to_string(document_.GetErrorOffset()))); | ||
| } | ||
| TRITONJSON_STATUSTYPE status = | ||
| ParseErrorHandler(document_, std::string(base, size)); |
There was a problem hiding this comment.
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.
Sure! Let me run it through CI then. I wanted your feedback first in case there was a reason not to do this.
Parse is used across the Triton codebase. When Parse is failed, all the user sees is the error type and index. To help with debugging, provide a preview of the parsed JSON.