Skip to content

Conversation

@spencewenski
Copy link
Contributor

@spencewenski spencewenski commented Aug 6, 2025

Problem

If an error occurs
in Leptos's middleware chain, the content-type header of the returned error response is not set.

Solution

Save the error's content-type from the boxed service in BoxedService, and pass the content type into Service::run.

Note that this requires the following breaking changes:

  • Add a new field to BoxedService.
  • Update the Service::run trait method to accept the error's content type as a parameter.

However, since this requires breaking changes anyway, this PR achieves
the following in a more extensible way (but still API-breaking):

  • Replace the ser field in BoxedService with a ServiceRunConfig that contains the ser error serializer and the error's content type string.
  • Mark BoxedService as #[non_exhaustive] so new fields can be added in the future without breaking the API.
  • Replace the ser parameter of Service::run with the ServiceRunConfig This allows Service::run to remain the same even if new fields are added to the ServiceRunConfig in the future.

Note: this is written on top of another PR that's currently open, so there are some changes in this PR that are originally from that PR.

Closes #4209

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.
Problem
-------
If an error occurs
[in Leptos's middleware chain](https://github.com/leptos-rs/leptos/blob/main/server_fn/src/middleware/mod.rs#L77),
the content-type header of the returned error response is not set.

Solution
--------
Save the error's content-type from the boxed service in
`BoxedService`, and pass the content type into `Service::run`.

Note that this requires the following breaking changes:
- Add a new field to `BoxedService`.
- Update the `Service::run` trait method to accept the error's content
  type as a parameter.

However, since this requires breaking changes anyway, this PR achieves
the following in a more extensible way (but still API-breaking):
- Replace the `ser` field in `BoxedService` with a `ServiceRunConfig`
  that contains the `ser` error serializer and the error's content
  type string.
- Mark `BoxedService` as `#[non_exhaustive]` so new fields can be added
  in the future without breaking the API.
- Replace the `ser` parameter of `Service::run` with the `ServiceRunConfig`
  This allows `Service::run` to remain the same even if
  new fields are added to the `ServiceRunConfig` in the future.
@spencewenski spencewenski changed the title fix: Set content type if error occurs in Leptos middleware fix!: Set content type if error occurs in Leptos middleware Aug 7, 2025
Copy link
Contributor

@benwis benwis left a comment

Choose a reason for hiding this comment

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

As mentioned in the other PR, let's refactor this to edit error_response()

@spencewenski
Copy link
Contributor Author

This is fixed by #4215 now since it was decided in that PR that it should be resolved in an API-breaking way.

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

2 participants