mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-25 13:40:19 +00:00
20d146d9bd
* improve documentation for the fullstack server context * Add a section about axum integration to the crate root docs * make serve_dioxus_application accept the cfg builder directly * remove unused server_fn module * improve fullstack config docs * improve documentation for the server function macro * fix axum router extension link * Fix doc tests * Fix launch builder * Simplify the launch builder * don't re-export launch in the prelude * refactor fullstack launch * Fix fullstack launch builder * Update static generation with the new builder api * fix some formatting/overly broad launch replacements * fix custom menu example * fix fullstack/static generation examples * Fix static generation launch * A few small formatting fixes * Fix a few doc tests * implement LaunchConfig for serve configs * fix fullstack launch with separate web and server launch methods * fix check with all features * dont expose inner core module * clippy and check * fix readme --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
161 lines
4.5 KiB
Rust
161 lines
4.5 KiB
Rust
//! This example showcases how to use the ErrorBoundary component to handle errors in your app.
|
|
//!
|
|
//! The ErrorBoundary component is a special component that can be used to catch panics and other errors that occur.
|
|
//! By default, Dioxus will catch panics during rendering, async, and handlers, and bubble them up to the nearest
|
|
//! error boundary. If no error boundary is present, it will be caught by the root error boundary and the app will
|
|
//! render the error message as just a string.
|
|
//!
|
|
//! NOTE: In wasm, panics can currently not be caught by the error boundary. This is a limitation of WASM in rust.
|
|
#![allow(non_snake_case)]
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
fn main() {
|
|
dioxus::launch(|| rsx! { Router::<Route> {} });
|
|
}
|
|
|
|
/// You can use an ErrorBoundary to catch errors in children and display a warning
|
|
fn Simple() -> Element {
|
|
rsx! {
|
|
GoBackButton { "Home" }
|
|
ErrorBoundary {
|
|
handle_error: |error: ErrorContext| rsx! {
|
|
h1 { "An error occurred" }
|
|
pre { "{error:#?}" }
|
|
},
|
|
ParseNumber {}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn ParseNumber() -> Element {
|
|
rsx! {
|
|
h1 { "Error handler demo" }
|
|
button {
|
|
onclick: move |_| {
|
|
// You can return a result from an event handler which lets you easily quit rendering early if something fails
|
|
let data: i32 = "0.5".parse()?;
|
|
|
|
println!("parsed {data}");
|
|
|
|
Ok(())
|
|
},
|
|
"Click to throw an error"
|
|
}
|
|
}
|
|
}
|
|
|
|
// You can provide additional context for the Error boundary to visualize
|
|
fn Show() -> Element {
|
|
rsx! {
|
|
GoBackButton { "Home" }
|
|
div {
|
|
ErrorBoundary {
|
|
handle_error: |errors: ErrorContext| {
|
|
rsx! {
|
|
for error in errors.errors() {
|
|
if let Some(error) = error.show() {
|
|
{error}
|
|
} else {
|
|
pre {
|
|
color: "red",
|
|
"{error}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
ParseNumberWithShow {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn ParseNumberWithShow() -> Element {
|
|
rsx! {
|
|
h1 { "Error handler demo" }
|
|
button {
|
|
onclick: move |_| {
|
|
let request_data = "0.5";
|
|
let data: i32 = request_data.parse()
|
|
// You can attach rsx to results that can be displayed in the Error Boundary
|
|
.show(|_| rsx! {
|
|
div {
|
|
background_color: "red",
|
|
border: "black",
|
|
border_width: "2px",
|
|
border_radius: "5px",
|
|
p { "Failed to parse data" }
|
|
Link {
|
|
to: Route::Home {},
|
|
"Go back to the homepage"
|
|
}
|
|
}
|
|
})?;
|
|
|
|
println!("parsed {data}");
|
|
|
|
Ok(())
|
|
},
|
|
"Click to throw an error"
|
|
}
|
|
}
|
|
}
|
|
|
|
// On desktop, dioxus will catch panics in components and insert an error automatically
|
|
fn Panic() -> Element {
|
|
rsx! {
|
|
GoBackButton { "Home" }
|
|
ErrorBoundary {
|
|
handle_error: |errors: ErrorContext| rsx! {
|
|
h1 { "Another error occurred" }
|
|
pre { "{errors:#?}" }
|
|
},
|
|
ComponentPanic {}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn ComponentPanic() -> Element {
|
|
panic!("This component panics")
|
|
}
|
|
|
|
#[derive(Routable, Clone, Debug, PartialEq)]
|
|
enum Route {
|
|
#[route("/")]
|
|
Home {},
|
|
#[route("/simple")]
|
|
Simple {},
|
|
#[route("/panic")]
|
|
Panic {},
|
|
#[route("/show")]
|
|
Show {},
|
|
}
|
|
|
|
fn Home() -> Element {
|
|
rsx! {
|
|
ul {
|
|
li {
|
|
Link {
|
|
to: Route::Simple {},
|
|
"Simple errors"
|
|
}
|
|
}
|
|
li {
|
|
Link {
|
|
to: Route::Panic {},
|
|
"Capture panics"
|
|
}
|
|
}
|
|
li {
|
|
Link {
|
|
to: Route::Show {},
|
|
"Show errors"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|