fix!: Set content type header for server function errors #4249
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When a server function returns an error, the content-type header is not set.
Solution
In order to set the content-type header in the response, the
FromServerFnError::Encoder::CONTENT_TYPE
associated constant needs to make its way to where the response object is constructed. This PR achieves this with the following changes:FromServerFnError#ser
trait method to return the content type as well as the serialized error response bytes. This is returned in a newServerFnErrorResponseParts
struct that contains both values and is marked#[non_exhaustive]
to allow adding new fields in the future without breaking semver.BoxedService#ser
field to be a function that returnsServerFnErrorResponseParts
to match the updatedFromServerFnError#ser
trait method. The field is also renamed toerr_ser
.BoxedService
as#[non_exhaustive]
to allow adding new fields in the future without breaking semver.Res#error_response
trait method to takeServerFnErrorResponseParts
instead ofBytes
.Res#error_response
to set the content-type header using the value fromServerFnErrorResponseParts
.bon
to auto-generate a builder API for theServerFnErrorResponseParts
struct.Note: I wasn't entirely sure how to handle
ServerFnErrorResponseParts
being used in stream/websocket responses, so I simply extracted thebody
field in order to match the existing behavior.This is a semver-incompatible version of #4215
Closes #4209