Skip to content

Commit

Permalink
get both client and server side working
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Jan 13, 2024
1 parent 31cee81 commit 30f3d67
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
37 changes: 21 additions & 16 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use leptos_router::*;
use parking_lot::RwLock;
use regex::Regex;
use server_fn::{
error::{NoCustomError, ServerFnErrorSerde, ServerFnUrlError},
error::{
NoCustomError, ServerFnErrorSerde, ServerFnUrlError,
SERVER_FN_ERROR_HEADER,
},
redirect::REDIRECT_HEADER,
request::actix::ActixRequest,
};
Expand Down Expand Up @@ -273,17 +276,20 @@ pub fn handle_server_fns_with_context(
let is_error = res.status()
== StatusCode::INTERNAL_SERVER_ERROR;
if is_error {
let (headers, body) = res.into_parts();
if let Ok(body) = body.try_into_bytes() {
if let Ok(body) =
String::from_utf8(body.to_vec())
{
// TODO allow other kinds?
if let Ok(err) = ServerFnUrlError::<
NoCustomError,
>::from_str(
&body
) {
if let Some(Ok(path)) = res
.headers()
.get(SERVER_FN_ERROR_HEADER)
.map(|n| n.to_str().map(|n| n.to_owned()))
{
let (headers, body) = res.into_parts();
if let Ok(body) = body.try_into_bytes() {
if let Ok(body) =
String::from_utf8(body.to_vec())
{
// TODO allow other kinds?
let err: ServerFnError<
NoCustomError,
> = ServerFnErrorSerde::de(&body);
if let Ok(referrer_str) =
referrer.to_str()
{
Expand All @@ -298,12 +304,11 @@ pub fn handle_server_fns_with_context(
.query_pairs_mut()
.append_pair(
"__path",
err.path(),
&path
)
.append_pair(
"__err",
&ServerFnErrorSerde::ser(err.error())
.unwrap_or_else(|_| err.error().to_string())
&ServerFnErrorSerde::ser(&err).unwrap_or_default()
);
let modified =
HeaderValue::from_str(
Expand All @@ -315,8 +320,8 @@ pub fn handle_server_fns_with_context(
}
}
}
res = headers.set_body(BoxBody::new(""))
}
res = headers.set_body(BoxBody::new(""))
};
*res.status_mut() = StatusCode::FOUND;
res.headers_mut().insert(LOCATION, referrer);
Expand Down
3 changes: 3 additions & 0 deletions server_fn/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::{
};
use thiserror::Error;

/// A custom header that can be used to indicate a server function returned an error.
pub const SERVER_FN_ERROR_HEADER: &'static str = "serverfnerror";

/// This is a result type into which any error can be converted,
/// and which can be used directly in your `view`.
///
Expand Down
8 changes: 6 additions & 2 deletions server_fn/src/response/actix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::Res;
use crate::error::{ServerFnError, ServerFnErrorErr, ServerFnUrlError};
use crate::error::{
ServerFnError, ServerFnErrorErr, ServerFnErrorSerde, ServerFnUrlError,
SERVER_FN_ERROR_HEADER,
};
use actix_web::{
http::{header, StatusCode},
HttpResponse,
Expand Down Expand Up @@ -77,7 +80,8 @@ where
fn error_response(path: &str, err: ServerFnError<CustErr>) -> Self {
ActixResponse(SendWrapper::new(
HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR)
.body(ServerFnUrlError::new(path, err).to_string()),
.append_header((SERVER_FN_ERROR_HEADER, path))
.body(err.ser().unwrap_or_else(|_| err.to_string())),
))
}
}

0 comments on commit 30f3d67

Please sign in to comment.