Skip to content

Conversation

spencewenski
Copy link
Contributor

@spencewenski spencewenski commented Aug 25, 2025

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:

  • [breaking] Update the FromServerFnError#ser trait method to return the content type as well as the serialized error response bytes. This is returned in a new ServerFnErrorResponseParts struct that contains both values and is marked #[non_exhaustive] to allow adding new fields in the future without breaking semver.
  • [breaking] Update the type of the BoxedService#ser field to be a function that returns ServerFnErrorResponseParts to match the updated FromServerFnError#ser trait method. The field is also renamed to err_ser.
  • [breaking] Mark BoxedService as #[non_exhaustive] to allow adding new fields in the future without breaking semver.
  • [breaking] Update the Res#error_response trait method to take ServerFnErrorResponseParts instead of Bytes.
  • Update the implementations of Res#error_response to set the content-type header using the value from ServerFnErrorResponseParts.
  • Add a dependency on bon to auto-generate a builder API for the ServerFnErrorResponseParts struct.

Note: I wasn't entirely sure how to handle ServerFnErrorResponseParts being used in stream/websocket responses, so I simply extracted the body field in order to match the existing behavior.

This is a semver-incompatible version of #4215

Closes #4209

spencewenski and others added 10 commits August 6, 2025 16:09
Problem
-------
When a server function return an error, the content type header is not
set.

Solution
--------
Use the `FromServerFnError::Encoder::CONTENT_TYPE` associated constant
to set the content type header on the returned API response.

Note: A similar issue occurs if an error is [returned in Leptos's
middleware
chain](https://github.com/leptos-rs/leptos/blob/b986fe11dc63a803d026f9ccae804e880c2a702b/server_fn/src/middleware/mod.rs#L77).
However, I believe resolving that will require adding a field to the
`BoxedService`, which would be an API-breaking change.
@spencewenski
Copy link
Contributor Author

Todo: Use typed-builder instead of bon as noted in this comment: #4215 (comment)

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.

Server functions do not set content-type header when an error is returned
1 participant