-
Hi All, I'm having some trouble with the #[component]
fn NumericInput(cx: Scope) -> impl IntoView {
let (value, set_value) = create_signal(cx, Ok(0));
- let on_input = move |ev| set_value(event_target_value(&ev).parse::<i32>());
+ // Changing the error type
+ let on_input = move |ev| set_value(event_target_value(&ev).parse::<i32>().map_err(|_| "error"));
view! { cx,
<h1>"Error Handling"</h1>
<label>
"Type a number (or something that's not a number!)"
<input on:input=on_input/>
<ErrorBoundary
// the fallback receives a signal containing current errors
fallback=|cx, errors| view! { cx,
<div class="error">
<p>"Not a number! Errors: "</p>
// we can render a list of errors as strings, if we'd like
<ul>
{move || errors.get()
.into_iter()
.map(|(_, e)| view! { cx, <li>{e.to_string()}</li>})
.collect_view(cx)
}
</ul>
</div>
}
>
<p>"You entered " <strong>{value}</strong></p>
</ErrorBoundary>
</label>
}
} Changing the error type to someting like
Thank you for the help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Hmm, I thought this might be the case, but this doesn't compile due to a similar error: #[derive(Debug, Error)]
enum MyError {
#[error("I'm an error")]
Error,
}
// Compiles
fn impls_into_error() -> leptos::error::Error {
MyError::Error.into()
}
#[component]
fn NumericInput(cx: Scope) -> impl IntoView {
let (value, set_value) = create_signal(cx, Ok(0));
// Added a map_err on this line
let on_input = move |ev| set_value.set(event_target_value(&ev).parse::<i32>().map_err(|_| MyError::Error));
view! { cx,
<h1>"Error Handling"</h1>
<label>
"Type a number (or something that's not a number!)"
<input on:input=on_input/>
<ErrorBoundary
// the fallback receives a signal containing current errors
fallback=|cx, errors| view! { cx,
<div class="error">
<p>"Not a number! Errors: "</p>
// we can render a list of errors as strings, if we'd like
<ul>
{move || errors.get()
.into_iter()
.map(|(_, e)| view! { cx, <li>{e.to_string()}</li>})
.collect_view(cx)
}
</ul>
</div>
}
>
<p>"You entered " <strong>{value}</strong></p>
</ErrorBoundary>
</label>
}
} |
Beta Was this translation helpful? Give feedback.
Hmm, I thought this might be the case, but this doesn't compile due to a similar error: