Skip to content

In connect unary protocol, fallback to code based on HTTP status if unable to deserialize it#702

Merged
jhump merged 2 commits intomainfrom
jh/handle-invalid-code
Mar 6, 2024
Merged

In connect unary protocol, fallback to code based on HTTP status if unable to deserialize it#702
jhump merged 2 commits intomainfrom
jh/handle-invalid-code

Conversation

@jhump
Copy link
Member

@jhump jhump commented Feb 28, 2024

The conformance tests, as of v1.0.0-rc3, currently require behavior that matches the current behavior of connect-go: if the error JSON body in a Connect unary response can be unmarshaled but is missing the code, it defaults to using "unknown" as the code.

However, in the Connect unary format (and not any of the others), we also have a non-200 HTTP status that could be used to determine a potentially better default than "unknown". So this PR changes it so that connect-go uses the HTTP status in this case, instead of always using "unknown". (Also see connectrpc/conformance#810.)

It also updates the JSON unmarshaling of the error body so it accepts a "code" property with an invalid value. In this case, the invalid value is discarded, and due to the change described above, the code will instead be computed from the HTTP status. But this lenience allows us to capture the message and details, in the event that the JSON contains these other properties but with (for example) a misspelled code string.

@jhump jhump requested a review from akshayjshah February 28, 2024 14:34
@jhump
Copy link
Member Author

jhump commented Feb 28, 2024

cc @srikrsna-buf

Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small asks, but the approach lgtm.

if wireErr.Code == 0 {
// code not set? default to one implied by HTTP status
wireErr.Code = connectHTTPToCode(response.StatusCode)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also open a PR to add this nuance to the wire protocol docs? This feels less like a behavioral specification and more like an aspect of the wire protocol itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roger that.

Copy link
Member Author

@jhump jhump Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was in the process of adding this and then noticed this existing paragraph in the section of the spec that describes the status code of the Unary-Response production:

When reading data from the wire, client implementations must use the HTTP-to-Connect mapping to infer a Connect error code if Bare-Message is missing or malformed.

IMO, this falls into this category. So it's arguable that always defaulting to "unknown" (as was previously done) was actually a bug.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you think the existing language needs an update or clarification?

@akshayjshah
Copy link
Contributor

akshayjshah commented Mar 6, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants