error example

This commit is contained in:
Greg Johnston 2024-02-19 14:23:52 -05:00
parent 13464b10c9
commit f584154156
4 changed files with 62 additions and 16 deletions

View file

@ -10,12 +10,15 @@
- [ ] hackernews
- [ ] counter\_isomorphic
- [ ] todo\_app\_sqlite
- ErrorBoundary
- ssr examples
- reactivity
- Signal wrappers
- SignalDispose implementations on all Copy types
- untracked access warnings
- ErrorBoundary
- [ ] RenderHtml implementation
- [ ] Separate component?
- Suspense/Transition components?
- callbacks
- unsync StoredValue
- SSR

View file

@ -1,6 +1,10 @@
use leptos::{
error::Result,
prelude::*,
reactive_graph::{computed::AsyncDerived, signal::signal},
reactive_graph::{
computed::AsyncDerived,
signal::{signal, RwSignal},
},
view, IntoView,
};
use serde::{Deserialize, Serialize};
@ -19,28 +23,25 @@ pub enum CatError {
type CatCount = usize;
// TODO: leptos::Result
async fn fetch_cats(count: CatCount) -> Option<Vec<String>> {
async fn fetch_cats(count: CatCount) -> Result<Vec<String>> {
if count > 0 {
// make the request
let res = reqwasm::http::Request::get(&format!(
"https://api.thecatapi.com/v1/images/search?limit={count}",
))
.send()
.await
.ok()?
.await?
// convert it to JSON
.json::<Vec<Cat>>()
.await
.ok()?
.await?
// extract the URL field for each cat
.into_iter()
.take(count)
.map(|cat| cat.url)
.collect::<Vec<_>>();
Some(res)
Ok(res)
} else {
None
Err(CatError::NonZeroCats)?
}
}
@ -75,10 +76,12 @@ pub fn fetch_example() -> impl IntoView {
let cats_view = move || {
async move {
cats.await
.into_iter()
.flatten()
.map(|s| view! { <p><img src={s}/></p> })
.collect::<Vec<_>>()
.map(|cats| {
cats.into_iter()
.map(|s| view! { <p><img src={s}/></p> })
.collect::<Vec<_>>()
})
.catch(|err| view! { <p class="error">{err.to_string()}</p> })
}
.suspend()
.transition()
@ -93,7 +96,7 @@ pub fn fetch_example() -> impl IntoView {
<input
type="number"
prop:value=move || cat_count.get().to_string()
on:input=move |ev| {
on:input:target=move |ev| {
let val = ev.target().value().parse::<CatCount>().unwrap_or(0);
set_cat_count.set(val);
}

View file

@ -164,6 +164,7 @@ pub use reactive_graph::{
self,
signal::{arc_signal, create_signal, signal},
};
pub use server_fn::error;
pub use show::*;
#[doc(hidden)]
pub use typed_builder;

View file

@ -1,4 +1,4 @@
use super::either::Either;
use super::{either::Either, RenderHtml};
use crate::view::{FallibleRender, Mountable, Render, Renderer};
use std::marker::PhantomData;
@ -135,6 +135,45 @@ where
}
}
// TODO RenderHtml implementation for ErrorBoundary
impl<T, Fal, FalFn, Rndr> RenderHtml<Rndr> for Try<T, Fal, FalFn, Rndr>
where
T: FallibleRender<Rndr>,
Fal: RenderHtml<Rndr>,
FalFn: FnMut(T::Error) -> Fal,
Rndr: Renderer,
Rndr::Element: Clone,
Rndr::Node: Clone,
{
const MIN_LENGTH: usize = Fal::MIN_LENGTH;
fn to_html_with_buf(
self,
buf: &mut String,
position: &mut super::Position,
) {
todo!()
}
fn to_html_async_with_buf<const OUT_OF_ORDER: bool>(
self,
buf: &mut crate::ssr::StreamBuilder,
position: &mut super::Position,
) where
Self: Sized,
{
todo!()
}
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &crate::hydration::Cursor<Rndr>,
position: &super::PositionState,
) -> Self::State {
todo!()
}
}
pub struct TryState<T, Fal, Rndr>
where
T: FallibleRender<Rndr>,