fix: set Content-Type header for server function errors (closes #4209) (#4215)

This commit is contained in:
Spencer Ferris
2025-08-29 05:56:26 -07:00
committed by GitHub
parent 7adb11ec49
commit 3b058e77f1
7 changed files with 48 additions and 9 deletions

View File

@@ -568,7 +568,7 @@ pub trait FromServerFnError: std::fmt::Debug + Sized + 'static {
/// Converts a [`ServerFnErrorErr`] into the application-specific custom error type.
fn from_server_fn_error(value: ServerFnErrorErr) -> Self;
/// Converts the custom error type to a [`String`].
/// Serializes the custom error type to bytes, according to the encoding given by `Self::Encoding`.
fn ser(&self) -> Bytes {
Self::Encoder::encode(self).unwrap_or_else(|e| {
Self::Encoder::encode(&Self::from_server_fn_error(
@@ -581,7 +581,7 @@ pub trait FromServerFnError: std::fmt::Debug + Sized + 'static {
})
}
/// Deserializes the custom error type from a [`&str`].
/// Deserializes the custom error type, according to the encoding given by `Self::Encoding`.
fn de(data: Bytes) -> Self {
Self::Encoder::decode(data).unwrap_or_else(|e| {
ServerFnErrorErr::Deserialization(e.to_string()).into_app_error()

View File

@@ -307,16 +307,18 @@ pub trait ServerFn: Send + Sized {
.await
.map(|res| (res, None))
.unwrap_or_else(|e| {
(
let mut response =
<<Self as ServerFn>::Server as crate::Server<
Self::Error,
Self::InputStreamError,
Self::OutputStreamError,
>>::Response::error_response(
Self::PATH, e.ser()
),
Some(e),
)
);
let content_type =
<Self::Error as FromServerFnError>::Encoder::CONTENT_TYPE;
response.content_type(content_type);
(response, Some(e))
});
// if it accepts HTML, we'll redirect to the Referer

View File

@@ -72,6 +72,10 @@ mod axum {
let inner = self.call(req);
Box::pin(async move {
inner.await.unwrap_or_else(|e| {
// TODO: This does not set the Content-Type on the response. Doing so will
// require a breaking change in order to get the correct encoding from the
// error's `FromServerFnError::Encoder::CONTENT_TYPE` impl.
// Note: This only applies to middleware errors.
let err =
ser(ServerFnErrorErr::MiddlewareError(e.to_string()));
Response::<Body>::error_response(&path, err)
@@ -149,6 +153,10 @@ mod actix {
let inner = self.call(req);
Box::pin(async move {
inner.await.unwrap_or_else(|e| {
// TODO: This does not set the Content-Type on the response. Doing so will
// require a breaking change in order to get the correct encoding from the
// error's `FromServerFnError::Encoder::CONTENT_TYPE` impl.
// Note: This only applies to middleware errors.
let err =
ser(ServerFnErrorErr::MiddlewareError(e.to_string()));
ActixResponse::error_response(&path, err).take()

View File

@@ -5,7 +5,7 @@ use crate::error::{
use actix_web::{
http::{
header,
header::{HeaderValue, LOCATION},
header::{HeaderValue, CONTENT_TYPE, LOCATION},
StatusCode,
},
HttpResponse,
@@ -80,6 +80,12 @@ impl Res for ActixResponse {
))
}
fn content_type(&mut self, content_type: &str) {
if let Ok(content_type) = HeaderValue::from_str(content_type) {
self.0.headers_mut().insert(CONTENT_TYPE, content_type);
}
}
fn redirect(&mut self, path: &str) {
if let Ok(path) = HeaderValue::from_str(path) {
*self.0.status_mut() = StatusCode::FOUND;

View File

@@ -100,6 +100,13 @@ impl Res for Response<Body> {
.unwrap()
}
fn content_type(&mut self, content_type: &str) {
if let Ok(content_type) = HeaderValue::from_str(content_type) {
self.headers_mut()
.insert(header::CONTENT_TYPE, content_type);
}
}
fn redirect(&mut self, path: &str) {
if let Ok(path) = HeaderValue::from_str(path) {
self.headers_mut().insert(header::LOCATION, path);

View File

@@ -60,6 +60,13 @@ impl Res for Response<Body> {
.unwrap()
}
fn content_type(&mut self, content_type: &str) {
if let Ok(content_type) = HeaderValue::from_str(content_type) {
self.headers_mut()
.insert(header::CONTENT_TYPE, content_type);
}
}
fn redirect(&mut self, path: &str) {
if let Ok(path) = HeaderValue::from_str(path) {
self.headers_mut().insert(header::LOCATION, path);

View File

@@ -37,9 +37,14 @@ where
/// Represents the response as created by the server;
pub trait Res {
/// Converts an error into a response, with a `500` status code and the error text as its body.
/// Converts an error into a response, with a `500` status code and the error as its body.
fn error_response(path: &str, err: Bytes) -> Self;
/// Set the `Content-Type` header for the response.
fn content_type(&mut self, #[allow(unused_variables)] content_type: &str) {
// TODO 0.9: remove this method and default implementation. It is only included here
// to allow setting the `Content-Type` header for error responses without requiring a
// semver-incompatible change.
}
/// Redirect the response by setting a 302 code and Location header.
fn redirect(&mut self, path: &str);
}
@@ -103,6 +108,10 @@ impl Res for BrowserMockRes {
unreachable!()
}
fn content_type(&mut self, _content_type: &str) {
unreachable!()
}
fn redirect(&mut self, _path: &str) {
unreachable!()
}