Skip to content
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

Open
DhZero opened this issue Apr 8, 2021 · 6 comments

Comments

@DhZero
Copy link

@DhZero DhZero commented Apr 8, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"

What 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 of switch?
Like this:

// addErrorContext returns a new error enhanced with information from d.errorContext
func (d *decodeState) addErrorContext(err error) error {
	if d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0 {
		var e *UnmarshalTypeError
		if errors.As(err, &e) {
			e.Struct = d.errorContext.Struct.Name()
			e.Field = strings.Join(d.errorContext.FieldStack, ".")
		}
	}
	return err
}
@rsc
Copy link
Contributor

@rsc rsc commented May 5, 2021

/cc @dsnet

Loading

@rsc
Copy link
Contributor

@rsc rsc commented May 5, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

Loading

@rsc rsc moved this from Incoming to Active in Proposals May 5, 2021
@networkimprov
Copy link

@networkimprov networkimprov commented May 15, 2021

cc @mvdan

Loading

@dsnet
Copy link
Member

@dsnet dsnet commented May 22, 2021

The idea of using errors.As instead of a direct type assertion seems reasonable at face value, but I think we should be careful about how we proceed here. Currently, the error reporting is already a bit non-sensible. I'd like to see #43126 be addressed first before addressing this.

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.

Loading

@rsc
Copy link
Contributor

@rsc rsc commented Jun 2, 2021

Putting on hold for #43126.
(If the investigation in #43126 ends up finding a suggested change then it would in turn become its own proposal.)

Loading

@rsc
Copy link
Contributor

@rsc rsc commented Jun 2, 2021

Placed on hold.
— rsc for the proposal review group

Loading

@rsc rsc moved this from Active to Hold in Proposals Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants