json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.33k stars 1.02k forks source link

Add typed IteratorError #685

Open btnmasher opened 1 year ago

btnmasher commented 1 year ago

This PR adds a typed IteratorError which enables consumers to check against jsonitor iterator errors via errors.As (directly or if the error itself is wrapped), as well as provide the additional context fields about the error to use in formatting their error messages in their preferred fashion.

This functionality is roughly comparable to stdlib's json.SyntaxError, which jsonitor does not currently have an equivalent. This has caused some headaches with my use of gin and it's JSON Binding mechanisms with gin's build tags set to supplant the stdlib json parser with jsonitor. Error reporting mechanisms which check for json.SyntaxErrors to aid in formatting user-presentable, helpful error messages in my APIs cannot easily detect jsonitor's iterator error messages because until this PR the error messages were untyped, generic error strings.

This change should be a transparent to existing consumers as the IteratorError type conforms to error interface with the same message syntax as prior to this change.