Compare commits

...

1 Commits

Author SHA1 Message Date
Greg Johnston
d270cd7da0 fix: errors on 404 page in axum_errors example 2023-02-07 07:20:48 -05:00
3 changed files with 14 additions and 7 deletions

View File

@@ -30,7 +30,7 @@ pub fn ErrorTemplate(
.into_iter() .into_iter()
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned()) .filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
.collect(); .collect();
println!("Errors: {errors:#?}"); log!("Errors: {errors:#?}");
// Only the response code for the first error is actually sent from the server // Only the response code for the first error is actually sent from the server
// this may be customized by the specific application // this may be customized by the specific application

View File

@@ -12,8 +12,7 @@ cfg_if! { if #[cfg(feature = "ssr")] {
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use std::sync::Arc; use std::sync::Arc;
use leptos::{LeptosOptions, Errors, view}; use leptos::{LeptosOptions, Errors, view};
use crate::error_template::{ErrorTemplate, ErrorTemplateProps}; use crate::landing::{App, AppProps};
use crate::errors::AppError;
pub async fn file_and_error_handler(uri: Uri, Extension(options): Extension<Arc<LeptosOptions>>, req: Request<Body>) -> AxumResponse { pub async fn file_and_error_handler(uri: Uri, Extension(options): Extension<Arc<LeptosOptions>>, req: Request<Body>) -> AxumResponse {
let options = &*options; let options = &*options;
@@ -23,9 +22,10 @@ cfg_if! { if #[cfg(feature = "ssr")] {
if res.status() == StatusCode::OK { if res.status() == StatusCode::OK {
res.into_response() res.into_response()
} else{ } else{
let mut errors = Errors::default(); let handler = leptos_axum::render_app_to_stream(
errors.insert_with_default_key(AppError::NotFound); options.to_owned(),
let handler = leptos_axum::render_app_to_stream(options.to_owned(), move |cx| view!{cx, <ErrorTemplate outside_errors=errors.clone()/>}); move |cx| view!{ cx, <App/> }
);
handler(req).await.into_response() handler(req).await.into_response()
} }
} }

View File

@@ -29,7 +29,14 @@ pub fn App(cx: Scope) -> impl IntoView {
cx, cx,
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/> <Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/errors_axum.css"/> <Stylesheet id="leptos" href="/pkg/errors_axum.css"/>
<Router> <Router fallback=|cx| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! { cx,
<ErrorTemplate outside_errors/>
}
.into_view(cx)
}>
<header> <header>
<h1>"Error Examples:"</h1> <h1>"Error Examples:"</h1>
</header> </header>