Skip to content

jsonpb: improve error reporting #266

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

Closed
mwitkow opened this issue Dec 7, 2016 · 14 comments
Closed

jsonpb: improve error reporting #266

mwitkow opened this issue Dec 7, 2016 · 14 comments

Comments

@mwitkow
Copy link

mwitkow commented Dec 7, 2016

JsonPB has very unhelpful error reporting. For example:

json: cannot unmarshal string into Go value of type []json.RawMessage

Its unclear what field this is about (and for multi nested messages it is a huge), and even the type []json.RawMessage is incorrect ([uint32]). I forked the jsonpb.Unmarshaller at go-nicejsonpb and introduced few improvements:

  • Errors are are now prefixed with a "stack" path of fields that are returned in (including offsets in arrays, and keys in maps)
  • Poor "cannot deserialize into `json.RawMessage" errors now use proper types
  • Unknown fields now return a helpful message listing known fields :)

For a comparison of outputs, here's a test against normal jsonpb with the expected values being from nicejsonpb

new: "unparsable field SomeIntRep.[3]: json: cannot unmarshal string into Go value of type uint32" 
old: "json: cannot unmarshal string into Go value of type uint32" 
	
new: "unparsable field SomeEmbedded.SomeValue: invalid character 's' looking for beginning of value while looking for an integer in a string"
old: "invalid character 's' looking for beginning of value"

new: "unparsable field SomeEmbedded.Identifier: json: cannot unmarshal number into Go value of type string" (expected)
old: "json: cannot unmarshal number into Go value of type string"

new: "unparsable field SomeIntRep: json: cannot unmarshal string into Go value of type []uint32"
old: "json: cannot unmarshal string into Go value of type []json.RawMessage"
	
new: "unparsable field SomeEmbedded: fields [someUnknown anotherUnknown] do not exist in set of known fields [identifier someValue]"
old: "unknown field \"someUnknown\" in validatortest.ValidatorMessage3_Embedded"

I'd happily upstream it, but would like to know whether introducing a new jsonpb.FieldError is a viable option for upstream. @bcmills, thoughts?

@mwitkow
Copy link
Author

mwitkow commented Feb 17, 2017

@bcmills any thoughts on upstreaming the fixes to jsonpb error reporting in https://github.com/mwitkow/go-nicejsonpb?

@bcmills
Copy link
Contributor

bcmills commented Feb 22, 2017

It probably would be a good idea to make error reporting clearer. How would your proposal fit in with #256?

@mwitkow
Copy link
Author

mwitkow commented Feb 27, 2017

@bcmills I commented there:
This proposal is compatible with whatever is proposed in #256. The same fieldStackError can be used in code-generated MarshalJSON and UnmarshalJSON functions as they would be be in the nicejsonpb functions: basically as we handle an error propagating up the call tree we add the stack information as to which field generated the error..

Thoughts?

@bcmills
Copy link
Contributor

bcmills commented Mar 3, 2017

It's a bit hard to tell what the proposed changes are because the commit history of go-nicejsonpb seems to be truncated, but I see a lot of fmt.Sprintf calls in the error construction path which seem like they'll add a lot of unnecessary allocations. Perhaps there's a more efficient way to structure the errors to give a better balance of overhead and clarity?

@mwitkow
Copy link
Author

mwitkow commented Mar 3, 2017 via email

@mwitkow
Copy link
Author

mwitkow commented Mar 15, 2017

@bcmills I raised #310 with the relevant changes. I do think that the allocations are not that big of a deal, as they're dealing with a relatively rare case of the problem arising in map keys.

@mwitkow
Copy link
Author

mwitkow commented May 17, 2017

@bcmills or @dsnet, can you please take a look at #310? :)

@dsnet
Copy link
Member

dsnet commented May 18, 2017

\cc @cybrcodr

@mwitkow
Copy link
Author

mwitkow commented Sep 24, 2017

@cybrcodr @dsnet any chance we could revisit this? :)

@jdaily
Copy link

jdaily commented Dec 7, 2017

I'm using a fork with the patch from #310 and it's been really useful. It would be nice to have support upstream :)

@dsnet dsnet changed the title jsonpb error reporting is poor, and here's how to fix it jsonpb: improve error reporting Feb 14, 2018
@dsnet dsnet added the api-v2 label Aug 2, 2018
@dsnet dsnet removed jsonpb labels Jul 10, 2019
@robaho
Copy link

robaho commented Jul 16, 2019

Is there some reason the PR has not been applied? Using jsonpb is extremely difficult in production with external providers as and error in the input stream is nearly impossible to diagnose. This is an example of re-using the error structs from 'json' and then 'zeroing' fields - when this package should probably define it's own error types.

@dsnet
Copy link
Member

dsnet commented Jul 16, 2019

This logic has been completely re-written in the next major release of Go protobufs. We are only applying bug fixes at this point to the current code base. Better error messages is nice-to-have, but is not a bug fix.

@robaho
Copy link

robaho commented Jul 18, 2019

Why can't you apply the PR as a back fix? Error diagnosis is nearly impossible with the current version. It is a real problem when dealing with external systems that may be generating the json not via protobufs.

And the bug is there for 3+ years?

@dsnet
Copy link
Member

dsnet commented Mar 3, 2020

The JSON implementation for protobuf has been completely rewritten in the v1.20.0 release release that this is unlikely to still be an issue. If the new error output is still insufficient, new issues can be filed.

@dsnet dsnet closed this as completed Mar 3, 2020
@golang golang locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants