Skip to content

Keep Response.Error when encoding a protobuf body - #187

Open
HakeemTheEmperor wants to merge 1 commit into
monzo:masterfrom
HakeemTheEmperor:fix/error-filter-protobuf-accept
Open

HakeemTheEmperor wants to merge 1 commit into
monzo:masterfrom
HakeemTheEmperor:fix/error-filter-protobuf-accept

Conversation

@HakeemTheEmperor

Copy link
Copy Markdown

Fixes #186

EncodeAsProtobuf, EncodeAsLegacyProtobuf and EncodeAsProtobufJSON ended with:

n, err := r.Write(b)
r.Error = terrors.Wrap(err, nil)

When the write succeeds, terrors.Wrap(nil, nil) returns nil, so any error already on the response was overwritten. ErrorFilter sets rsp.Error and then encodes the terror into the body, and with Accept: application/protobuf that goes through EncodeAsLegacyProtobuf. Failed requests from protobuf clients therefore came back with rsp.Error == nil, and Decode would read the terror into the caller's message.

This changes the three methods to only set r.Error when Write fails, which is what EncodeAsJSON already does.

Tests:

  • TestResponseProtobufEncodersKeepExistingError checks that each of the three encoders keeps an existing error
  • TestErrorFilterKeepsErrorForAllAcceptTypes checks that ErrorFilter keeps the error for both JSON and protobuf Accept headers

Both fail on master and pass with this change. go vet ./... and go test ./... pass.

EncodeAsProtobuf, EncodeAsLegacyProtobuf and EncodeAsProtobufJSON set r.Error = terrors.Wrap(err, nil) even when the write succeeded, which overwrote any error already on the response with nil. ErrorFilter hits this when the request has Accept: application/protobuf, so failed requests came back with a nil error.
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.

ErrorFilter drops rsp.Error when the request has Accept: application/protobuf

1 participant