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

json.Encoder.Encode does not return error from underlying writer #139

Open
geoff-ziprecruiter-com opened this issue Jun 24, 2024 · 0 comments

Comments

@geoff-ziprecruiter-com
Copy link

If the writer encounters an error the error is stored in the encoder, but it is not returned.

if _, err := enc.writer.Write(b); err != nil {

This is because the above line is creating a new err variable due to the : in the assignment.

This test code demonstrates the issue:

type errWriter struct{}

func (ew *errWriter) Write([]byte) (n int, err error) {
	return 0, errors.New("test-error")
}

func TestWriterErr(t *testing.T) {
	t.Parallel()

	enc := NewEncoder(&errWriter{})

	err := enc.Encode("")

	if err == nil {
		t.Error("expected error not returned")
	} else if err.Error() != "test-error" {
		t.Errorf("unexpected error returned: want=test-error got=%s", err.Error())
	}
}
vmihailenco added a commit to vmihailenco/encoding that referenced this issue Oct 5, 2024
vmihailenco added a commit to vmihailenco/encoding that referenced this issue Oct 5, 2024
vmihailenco added a commit to vmihailenco/encoding that referenced this issue Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant