style and handle more errors

This commit is contained in:
Adam 2024-01-18 19:51:47 -05:00
parent 483d7eea1d
commit 959b2f3071

View file

@ -1,3 +1,5 @@
use std::any::type_name;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use http::status::StatusCode; use http::status::StatusCode;
use leptos::*; use leptos::*;
@ -20,8 +22,6 @@ impl AppError {
} }
} }
// A basic function to display errors served by the error boundaries.
// Feel free to do more complicated things here than just displaying the error.
#[component] #[component]
pub fn ErrorTemplate( pub fn ErrorTemplate(
#[prop(optional)] outside_errors: Option<Errors>, #[prop(optional)] outside_errors: Option<Errors>,
@ -37,38 +37,26 @@ pub fn ErrorTemplate(
// Get Errors from Signal // Get Errors from Signal
let errors = errors.get_untracked(); let errors = errors.get_untracked();
// Downcast lets us take a type that implements `std::error::Error` let errors: Box<_> = errors.into_iter().filter_map(|(_k, v)| v.into()).collect();
let errors: Vec<AppError> = errors
.into_iter()
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
.collect();
println!("Errors: {errors:#?}"); println!("Errors: {errors:#?}");
// Only the response code for the first error is actually sent from the server
// this may be customized by the specific application
cfg_if! { if #[cfg(feature="ssr")] {
let response = use_context::<ResponseOptions>();
if let Some(response) = response {
response.set_status(errors[0].status_code());
}
}}
view! { view! {
<h1>{if errors.len() > 1 { "Errors" } else { "Error" }}</h1> <article class="p-7 my-5 mx-auto w-11/12 max-w-screen-xl bg-opacity-10 rounded-md bg-zinc-700 shadow-1g">
<For <h1 class="text-3xl font-light text-orange-600 capitalize max-6-xs">
// a function that returns the items we're iterating over; a signal is fine {if errors.len() > 1 { "Errors!" } else { "Error!" }}
each=move || { errors.clone().into_iter().enumerate() } </h1>
// a unique key for each item as a reference <hr class="opacity-50"/>
key=|(index, _error)| *index
// renders each item to a view <ul>
children=move |error| { {move || {
let error_string = error.1.to_string(); errors
let error_code = error.1.status_code(); .into_iter()
view! { .map(|e: &_| view! { <li>{e.to_string()}</li> })
<h2>{error_code.to_string()}</h2> .collect_view()
<p>"Error: " {error_string}</p> }}
}
} </ul>
/>
</article>
} }
} }