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

encoding/json: error message for nested encoding is not specific enough #34327

Closed
rohitkeshwani07 opened this issue Sep 16, 2019 · 1 comment
Closed

Comments

@rohitkeshwani07
Copy link

go version go1.12.6

type Response2 struct {
	Page   int       `json:"page"`
	Fruits []Response2 `json:"fruits"`
}

func TestSomething1(t *testing.T) {
	byt := []byte(`{"page":6,"fruits":[{"page":6,"fruits":[{"page":6,"fruits":""}]}]}`)

	var dat Response2

	if err := json.Unmarshal(byt, &dat); err != nil {
		panic(err)
	}

	fmt.Println(dat)
}

for the above code, error message return by library is

json: cannot unmarshal string into Go struct field Response2.fruits of type []dtos.Response2

but better message could have been

json: cannot unmarshal string into Go struct field Response2.fruits[0].fruits[0].fruits of type []dtos.Response2
@smasher164 smasher164 changed the title No proper Error Message for Nested encoding encoding/json: error message for nested encoding is not specific enough Sep 16, 2019
@smasher164
Copy link
Member

The error message in 1.13 is much better (https://play.golang.org/p/UD-bFXMEWHh) with

panic: json: cannot unmarshal string into Go struct field Response2.fruits.fruits.fruits of type []main.Response2

I think this commit (29bc4f1) fixes this, but I don't think it is worth backporting an error message improvement.

@golang golang locked and limited conversation to collaborators Sep 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants