-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
encoding/json: use different error type for unknown field if they are disallowed #40982
Comments
I understand that this should go through the proposal process, but any idea to start that? |
@Segflow See https://golang.org/s/proposal. To make a proposal, please propose a specific change. Thanks. |
First I think we want to hear what @mvdan thinks of the idea... |
Given the existence of custom Furthermore, the original poster should adequately explain what they want to use this for. It's rare for any proposal to go through without some justification for why it's useful. |
@dsnet one of our services process messages from a queue, there could be multiple instances reading from the same queue. Once a service reads a message from the dequeue we decode it(json) and process it. A message could be invalid, thus a service should decide whenever it should delete the message from the queue or keep it so that probably another instance can process it (probably a newer version of the service). For instance a "SyntaxError" on a message means we can safely delete the message. We currently check if error starts with |
I also some similar usage in other repo: https://github.com/search?q=%22json%3A+unknown+field%22+language%3AGo&type=Code&ref=advsearch&l=Go&l= |
If you want to keep the message if it is syntactically valid, is there a reason why |
Following up a bit more, a problem with relying on errors to determine attributes about the input is the difficulty in presenting that information well and the confusion that arises when scaling to multiple attributes that a user may want to distinguish. For protocol buffers, we had this issue where we wanted to present errors to distinguish between inputs that were "missing required fields" and inputs that contained "invalid UTF-8". Let's suppose the previous error was distinguishable using Now suppose an input had both missing required fields and invalid UTF-8, which error do you present? Do you arbitrarily present just one of the two errors or do you return a composite error where
The |
@dsnet - things may have changed a bit now with Our case is that we want to present a specific message format to the caller. The json package already has two structured error types. Not having one in this error case is inconsistent. Our use case is something like the following:
|
On Go 1.15 and below, json decoder returns a generic error when unknown fields are disallowed (via
Decoder.DisallowUnknownFields
. See https://github.com/golang/go/blob/master/src/encoding/json/decode.go#L737-L739It may be useful to return a different error type so the caller can detect when it is the case.
The only way to detect that currently is to interpret the string returned by
Error
The text was updated successfully, but these errors were encountered: