Skip to content
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

Feature request: more options for reporting errors #992

Open
bwbecker opened this issue Feb 12, 2023 · 3 comments
Open

Feature request: more options for reporting errors #992

bwbecker opened this issue Feb 12, 2023 · 3 comments
Labels

Comments

@bwbecker
Copy link

Hi -- Just getting acquainted with jsoniter. Looks like a lovely library; thanks for the efforts.

However (you knew that was coming, right?), I wish there were more options for reporting errors than the message from JsonReaderException:

missing required field "student_take_units", offset: 0x00007255, buf:
+----------+-------------------------------------------------+------------------+
|          |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f | 0123456789abcdef |
+----------+-------------------------------------------------+------------------+
| 00007230 | 73 74 75 64 65 6e 74 5f 74 61 6b 65 5f 75 6e 69 | student_take_uni |
| 00007240 | 74 73 58 58 58 22 3a 20 30 2e 31 33 0a 20 20 20 | tsXXX": 0.13.    |
| 00007250 | 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 20 20 |      },.         |
| 00007260 | 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73 | {.            "s |
| 00007270 | 75 62 6a 65 63 74 5f 63 6f 64 65 22 3a 20 22 57 | ubject_code": "W |
+----------+-------------------------------------------------+------------------+

This isn't a very user-friendly error message.

What I'd love is easy ways to get the error message ("missing required field.."), the offset as an integer line number plus offset into the line (base 10), and access to the buffer so I can format it as appropriate for my users.

@plokhotnyuk
Copy link
Owner

Hi, @bwbecker!

Thanks for trying jsoniter-scala and sending feedback here!

The library developed to be safe, correct and performant so we cannot implement roughly defined or costly in runtime features.

But I will happy to help you solve any concrete use case.

Feel free to reach me here or in the Gitter chat for any question or discussion.

@bwbecker
Copy link
Author

Thanks for the quick reply, @plokhotnyk!

My concrete use case is to provide a comprehensible error message to an end user rather than to a developer. The end user isn't going to understand a hex offset and the hex dump will scare them off of even trying to read the error message.

Currently I'm parsing the error message from JsonReaderException to get just the error message and hex offset. I then read the file until the offset, counting lines so I can present something like this to the user:

missing required field "student_take_units" @ line 959
957             "student_take_unitsXXX": 0.13
958         },
959         {
960             "subject_code": "WKRPT",

I understand that the input may not always have distinct lines, like mine tend to, so an offset into the line would also be important.

But the main point is that the information should be accessible in the JsonReaderException object without parsing the error message. The offset is obviously already available. I think all the information is readily accessible when JsonReaderException is thrown at line 773 in JsonReader.scala (the decodeError method) except for the line number. I'd guess that counting the lines would have a very minor impact on performance. As for the buffer, perhaps just provide it as a string with information about the line & offset where it starts.

Thanks for considering this suggestion!

@plokhotnyuk
Copy link
Owner

@bwbecker You are right that error messages are formatted for developers (not for users who afraid hex dumps ;)

I see you have a workaround by rereading an affected file, counting lines until the error offset and collecting required lines for the final error for your users.

But I don't see how the proposed built-in line counter can be implemented without significant extra penalty in runtime and without severe security risks for cases when malicious counterpart will use weakness of line collecting implementation.

Feel free to propose a PR if you see some safe and efficient implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants