Skip to content

json: return writer errors from Encoder.Encode#163

Open
c-tonneslan wants to merge 1 commit into
segmentio:masterfrom
c-tonneslan:fix/encoder-encode-writer-error
Open

json: return writer errors from Encoder.Encode#163
c-tonneslan wants to merge 1 commit into
segmentio:masterfrom
c-tonneslan:fix/encoder-encode-writer-error

Conversation

@c-tonneslan
Copy link
Copy Markdown

The inner if _, err := enc.writer.Write(b) shadowed the outer err with :=, so the writer error got cached on enc.err but the function still returned the (nil) outer err. The first Encode after a write failure looked like a success; only the next call would surface the cached error via the early return at the top.

Switching to = so the outer err actually receives the writer error.

Test fails on master, passes here. Closes #139.

The inner `if _, err := enc.writer.Write(b)` shadowed the outer err with
:=, so the writer error got stored on enc.err but the function still
returned the (nil) outer err. First Encode after a write failure would
look like it succeeded; only the next call would surface the error via
the enc.err early-return at the top.

Switch the assignment to = so the outer err is set, then returned.

Closes segmentio#139

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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

Successfully merging this pull request may close these issues.

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

1 participant