Skip to content

Commit

Permalink
docs/warnings: improve ServerFnError when a server function is not …
Browse files Browse the repository at this point in the history
…found (#1350)
  • Loading branch information
gbj authored Jul 14, 2023
1 parent 10d51a8 commit 3eed86f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
7 changes: 6 additions & 1 deletion integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ pub fn handle_server_fns_with_context(
} else {
HttpResponse::BadRequest().body(format!(
"Could not find a server function at the route {:?}. \
\n\nIt's likely that you need to call \
\n\nIt's likely that either
1. The API prefix you specify in the `#[server]` \
macro doesn't match the prefix at which your server \
function handler is mounted, or \n2. You are on a \
platform that doesn't support automatic server \
function registration and you need to call \
ServerFn::register_explicit() on the server function \
type, somewhere in your `main` function.",
req.path()
Expand Down
9 changes: 7 additions & 2 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,14 @@ async fn handle_server_fns_inner(
Response::builder().status(StatusCode::BAD_REQUEST).body(
Full::from(format!(
"Could not find a server function at the route \
{fn_name}. \n\nIt's likely that you need to call \
{fn_name}. \n\nIt's likely that either
1. The API prefix you specify in the `#[server]` \
macro doesn't match the prefix at which your server \
function handler is mounted, or \n2. You are on a \
platform that doesn't support automatic server \
function registration and you need to call \
ServerFn::register_explicit() on the server function \
type, somewhere in your `main` function."
type, somewhere in your `main` function.",
)),
)
}
Expand Down
11 changes: 9 additions & 2 deletions integrations/viz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,17 @@ async fn handle_server_fns_inner(
.body(Body::from(format!(
"Could not find a server function at the \
route {fn_name}. \n\nIt's likely that \
you need to call \
either
1. The API prefix you specify in the \
`#[server]` macro doesn't match the \
prefix at which your server function \
handler is mounted, or \n2. You are on a \
platform that doesn't support automatic \
server function registration and you \
need to call \
ServerFn::register_explicit() on the \
server function type, somewhere in your \
`main` function."
`main` function.",
)))
}
.expect("could not build Response");
Expand Down
10 changes: 10 additions & 0 deletions server_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ where
#[cfg(not(target_arch = "wasm32"))]
let binary = binary.as_ref();

if status == 400 {
return Err(ServerFnError::ServerError(
"No server function was found at this URL.".to_string(),
));
}

ciborium::de::from_reader(binary)
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
} else {
Expand All @@ -616,6 +622,10 @@ where
.await
.map_err(|e| ServerFnError::Deserialization(e.to_string()))?;

if status == 400 {
return Err(ServerFnError::ServerError(text));
}

let mut deserializer = JSONDeserializer::from_str(&text);
T::deserialize(&mut deserializer)
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
Expand Down

0 comments on commit 3eed86f

Please sign in to comment.