-
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
proposal: encoding/json: wrap error from TextUnmarshaler/Unmarshaler to locate the problem in input #58655
Comments
Unfortunately, this is a breaking change, since currently people could be doing an equality check ( |
See https://github.com/go-json-experiment/json in which some members of the Go team were working on an experiment to create a new version of encoding/json. ISTM as an outsider that an improvements to encoding/json will have to run through that package before getting into the standard library. |
@carlmjohnson thanks for pointing me there. That is a large redesign, and I guess no one has any idea if/when that would be available in
The @rittneje thanks for noting that, I didn't even know there were times without With that, the only non-breaking change that comes to mind is adding a setting to json.Decoder similar to |
This feature would be especially helpful for types implementing The usefulness of my own package |
Issue:
Currently, unmarshaling json with
json.Unmarshal()
(orjson.NewDecoder().Decode()
), when a value is being unmarshaled into a struct field that implementsencoding.TextUnmarshaler
(or thejson.Unmarshaler
) and the.UnmarshalText()
returns an error, this error is returned unchanged from the wholejson.Unmarshal
function:https://go.dev/play/p/G5ag-Cq2OMf
output:
This way, caller has no way to locate the error in input json when the input is a bit larger / contains multiple fields, some of them nested.
Compare to a similar error when a type in input json is not compatible with the struct field type:
https://go.dev/play/p/ZgexFrs-r_c
output:
In this case, the returned error contains a lot of useful information to locate the problem in intput, specifically a
Field
containing a path in json to the problematic value.Proposal:
In a similar way to
json.UnmarshalTypeError
, when thejson.Unmarshal
gets an error from either.UnmarshalText()
or.UnmarshalJSON()
, the error would be wrapped into a similar structure, for example (currenlty not existing, proposed error)&json.UnmarshalerError{Type:(*reflect.rtype)(0x4b5460), Offset:19, Struct:"C", Field:"b.cs.d", Err: &errors.errorString{s:"test err"}}
. Compared tojson.UnmarshalTypeError
, theValue
is removed, as it might be not useful (and if needed can be added by the field type's unmarshal method), butErr
with the original error is added, so that the original error is stillIs
able/As
able(Also, the
Field:"b.cs.d"
could beField:"b.cs[0].d"
to better locate error in json lists, but to remain consistent withjson.UnmarshalTypeError
, it would be the same, that isField:"b.cs.d"
)Note
I see a very similar proposal was submitted very recently - returning a more friendly error, although in a different scenario (disallowed unknown field): #58649
So maybe the solution could be somewhat similar (for each case a special error type?) and could be possibly implemented together
The text was updated successfully, but these errors were encountered: