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

Add JSONPointer to SyntacticError #66

Merged
merged 1 commit into from
Dec 9, 2024
Merged

Add JSONPointer to SyntacticError #66

merged 1 commit into from
Dec 9, 2024

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Dec 7, 2024

Make SyntacticError precise about the position of the error by also recording the JSON pointer.

Other changes:

  • Make SyntacticError wrap an error.
  • Export ErrDuplicateName as a means to programmatically identify a duplicate object name error.
  • Export ErrNonStringName to indicate a syntax error when the expected JSON object name is not a string.
  • Always wrap io.ErrUnexpectedEOF in SyntacticError.
  • Avoid any dependency injection between jsontext and jsonwire.
  • Add convenience methods to jsontext.Pointer.

@dsnet dsnet force-pushed the errors-syntactic branch 4 times, most recently from ef155d6 to f0592aa Compare December 9, 2024 01:32
Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool - my only comments are that we should add some examples of using the syntactic error to extract information about the error. Maybe we have some and I missed it?

@dsnet
Copy link
Collaborator Author

dsnet commented Dec 9, 2024

Examples can come later. This is the first in a sequence to improve error reporting.

After this change, we now prints errors like:

  • jsontext: unexpected EOF after offset 13
  • jsontext: invalid UTF-8 within "/fizz/buzz" after offset 192
  • jsontext: invalid character 'E' within literal false (expecting 'e') after offset 12
  • jsontext: duplicate object name "foo" within "/fizz/buzz"

Of particular note, we specially handled ErrDuplicateName in the SyntacticError.Error method for better readability. Without special handling, it would print as jsontext: duplicate object name within "/fizz/buzz/foo", which is technically correct, but fails to highlight "foo" as the offending name.

Make SyntacticError precise about the precise location of the error
by also recording the JSON pointer.

Other changes:
* Make SyntacticError wrap an error.
* Export ErrDuplicateName as a means to programmatically
  identify a duplicate object name error.
* Export ErrNonStringName to indicate a syntax error
  when the expected JSON object name is not a string.
* Always wrap io.ErrUnexpectedEOF in SyntacticError since
  truncation of JSON text is technically a syntactic error.
* Avoid any dependency injection between jsontext and jsonwire.
* Add convenience methods to jsontext.Pointer.
@dsnet dsnet merged commit cef7d84 into master Dec 9, 2024
8 checks passed
@dsnet dsnet deleted the errors-syntactic branch December 9, 2024 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants