fix: correctly pass server fn errors to client (#822)

This commit is contained in:
Greg Johnston
2023-04-07 08:12:10 -04:00
committed by GitHub
parent 41a5e09caa
commit ff5ceddbe2
8 changed files with 19 additions and 5 deletions

View File

@@ -14,5 +14,6 @@ leptos = { workspace = true, features = ["ssr"] }
leptos_meta = { workspace = true, features = ["ssr"] }
leptos_router = { workspace = true, features = ["ssr"] }
leptos_integration_utils = { workspace = true }
serde_json = "1"
parking_lot = "0.12.1"
regex = "1.7.0"

View File

@@ -264,8 +264,10 @@ pub fn handle_server_fns_with_context(
}
}
}
Err(e) => HttpResponse::InternalServerError()
.body(e.to_string()),
Err(e) => HttpResponse::InternalServerError().body(
serde_json::to_string(&e)
.unwrap_or_else(|_| e.to_string()),
),
}
} else {
HttpResponse::BadRequest().body(format!(

View File

@@ -16,5 +16,6 @@ leptos = { workspace = true, features = ["ssr"] }
leptos_meta = { workspace = true, features = ["ssr"] }
leptos_router = { workspace = true, features = ["ssr"] }
leptos_integration_utils = { workspace = true }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
parking_lot = "0.12.1"

View File

@@ -411,7 +411,10 @@ async fn handle_server_fns_inner(
}
Err(e) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::from(e.to_string())),
.body(Full::from(
serde_json::to_string(&e)
.unwrap_or_else(|_| e.to_string()),
)),
}
} else {
Response::builder()

View File

@@ -16,5 +16,6 @@ leptos = { workspace = true, features = ["ssr"] }
leptos_meta = { workspace = true, features = ["ssr"] }
leptos_router = { workspace = true, features = ["ssr"] }
leptos_integration_utils = { workspace = true }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
parking_lot = "0.12.1"

View File

@@ -301,7 +301,10 @@ async fn handle_server_fns_inner(
}
Err(e) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(e.to_string())),
.body(Body::from(
serde_json::to_string(&e)
.unwrap_or_else(|_| e.to_string()),
)),
}
} else {
Response::builder()

View File

@@ -94,6 +94,7 @@ use std::{
#[cfg(any(feature = "ssr", doc))]
type ServerFnTraitObj = server_fn::ServerFnTraitObj<Scope>;
#[allow(unused)]
type ServerFunction = server_fn::ServerFunction<Scope>;
#[cfg(any(feature = "ssr", doc))]

View File

@@ -515,11 +515,13 @@ where
#[cfg(not(target_arch = "wasm32"))]
let status = status.as_u16();
if (500..=599).contains(&status) {
let text = resp.text().await.unwrap_or_default();
#[cfg(target_arch = "wasm32")]
let status_text = resp.status_text();
#[cfg(not(target_arch = "wasm32"))]
let status_text = status.to_string();
return Err(ServerFnError::ServerError(status_text));
return Err(serde_json::from_str(&text)
.unwrap_or(ServerFnError::ServerError(status_text)));
}
// Decoding the body of the request