2024-02-14 20:33:07 +00:00
|
|
|
//! 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.
|
|
|
|
|
2024-01-16 02:02:58 +00:00
|
|
|
use dioxus::{dioxus_core::CapturedError, prelude::*};
|
2022-03-08 19:15:18 +00:00
|
|
|
|
|
|
|
fn main() {
|
2024-01-16 17:45:02 +00:00
|
|
|
launch_desktop(app);
|
2022-03-08 19:15:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 21:06:05 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-11-08 00:24:07 +00:00
|
|
|
ErrorBoundary {
|
2024-02-14 20:33:07 +00:00
|
|
|
handle_error: |error: CapturedError| rsx! {
|
|
|
|
h1 { "An error occurred" }
|
|
|
|
pre { "{error:#?}" }
|
|
|
|
},
|
2024-01-14 05:12:21 +00:00
|
|
|
DemoC { x: 1 }
|
2022-03-08 19:15:18 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-11-16 09:13:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn DemoC(x: i32) -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-02-14 20:33:07 +00:00
|
|
|
h1 { "Error handler demo" }
|
|
|
|
button {
|
|
|
|
onclick: move |_| {
|
|
|
|
// Create an error
|
|
|
|
let result: Result<Element, &str> = Err("Error");
|
|
|
|
|
|
|
|
// And then call `throw` on it. The `throw` method is given by the `Throw` trait which is automatically
|
|
|
|
// imported via the prelude.
|
|
|
|
_ = result.throw();
|
|
|
|
},
|
|
|
|
"Click to throw an error"
|
|
|
|
}
|
2024-01-11 20:11:27 +00:00
|
|
|
}
|
2022-03-08 19:15:18 +00:00
|
|
|
}
|