-
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: add support of wrapped error in addErrorContext function #45449
Comments
/cc @dsnet |
This proposal has been added to the active column of the proposals project |
cc @mvdan |
The idea of using The reason I see #43126 as relevant is because it's unclear whether the error is supposed to report the location as 1) simply a struct field in a Go struct, or 2) the JSON path from the root JSON value. Currently, it is an odd mix of both. Depending on which information we intend to report, that changes how error wrapping would be performed. |
Placed on hold. |
Another issue happening on this same function is that when a custom error is returned, and this error does not wrap any JSON errors, I lose context on which field the error happened. If possible, please wrap all error types for keeping this context. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/fwgQFqafa3S
What did you expect to see?
Expected to see proper values set to err.Field and err.Struct even if error was wrapped by
fmt.Errorf()
in UnmarshalJSON of custom structure.What did you see instead?
In decode.go in addErrorContext function,
switch
won't work as expected because type mismatch.So err.Field and err.Struct will be empty even if
d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0
condition was passed.What did you propose?
Maybe it is better to use
errors.As()
instead ofswitch
?Like this:
The text was updated successfully, but these errors were encountered: