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

@rohitkeshwani07 rohitkeshwani07 commented Sep 16, 2019

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

This comment has been minimized.

Copy link
Member

@smasher164 smasher164 commented Sep 16, 2019

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.

@smasher164 smasher164 closed this Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.