errors.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //! This example showcases how to use the ErrorBoundary component to handle errors in your app.
  2. //!
  3. //! The ErrorBoundary component is a special component that can be used to catch panics and other errors that occur.
  4. //! By default, Dioxus will catch panics during rendering, async, and handlers, and bubble them up to the nearest
  5. //! error boundary. If no error boundary is present, it will be caught by the root error boundary and the app will
  6. //! render the error message as just a string.
  7. //!
  8. //! NOTE: In wasm, panics can currently not be caught by the error boundary. This is a limitation of WASM in rust.
  9. #![allow(non_snake_case)]
  10. use dioxus::prelude::*;
  11. fn main() {
  12. dioxus::launch(|| rsx! { Router::<Route> {} });
  13. }
  14. /// You can use an ErrorBoundary to catch errors in children and display a warning
  15. fn Simple() -> Element {
  16. rsx! {
  17. GoBackButton { "Home" }
  18. ErrorBoundary {
  19. handle_error: |error: ErrorContext| rsx! {
  20. h1 { "An error occurred" }
  21. pre { "{error:#?}" }
  22. },
  23. ParseNumber {}
  24. }
  25. }
  26. }
  27. #[component]
  28. fn ParseNumber() -> Element {
  29. rsx! {
  30. h1 { "Error handler demo" }
  31. button {
  32. onclick: move |_| {
  33. // You can return a result from an event handler which lets you easily quit rendering early if something fails
  34. let data: i32 = "0.5".parse()?;
  35. println!("parsed {data}");
  36. Ok(())
  37. },
  38. "Click to throw an error"
  39. }
  40. }
  41. }
  42. // You can provide additional context for the Error boundary to visualize
  43. fn Show() -> Element {
  44. rsx! {
  45. GoBackButton { "Home" }
  46. div {
  47. ErrorBoundary {
  48. handle_error: |errors: ErrorContext| {
  49. rsx! {
  50. for error in errors.errors() {
  51. if let Some(error) = error.show() {
  52. {error}
  53. } else {
  54. pre {
  55. color: "red",
  56. "{error}"
  57. }
  58. }
  59. }
  60. }
  61. },
  62. ParseNumberWithShow {}
  63. }
  64. }
  65. }
  66. }
  67. #[component]
  68. fn ParseNumberWithShow() -> Element {
  69. rsx! {
  70. h1 { "Error handler demo" }
  71. button {
  72. onclick: move |_| {
  73. let request_data = "0.5";
  74. let data: i32 = request_data.parse()
  75. // You can attach rsx to results that can be displayed in the Error Boundary
  76. .show(|_| rsx! {
  77. div {
  78. background_color: "red",
  79. border: "black",
  80. border_width: "2px",
  81. border_radius: "5px",
  82. p { "Failed to parse data" }
  83. Link {
  84. to: Route::Home {},
  85. "Go back to the homepage"
  86. }
  87. }
  88. })?;
  89. println!("parsed {data}");
  90. Ok(())
  91. },
  92. "Click to throw an error"
  93. }
  94. }
  95. }
  96. // On desktop, dioxus will catch panics in components and insert an error automatically
  97. fn Panic() -> Element {
  98. rsx! {
  99. GoBackButton { "Home" }
  100. ErrorBoundary {
  101. handle_error: |errors: ErrorContext| rsx! {
  102. h1 { "Another error occurred" }
  103. pre { "{errors:#?}" }
  104. },
  105. ComponentPanic {}
  106. }
  107. }
  108. }
  109. #[component]
  110. fn ComponentPanic() -> Element {
  111. panic!("This component panics")
  112. }
  113. #[derive(Routable, Clone, Debug, PartialEq)]
  114. enum Route {
  115. #[route("/")]
  116. Home {},
  117. #[route("/simple")]
  118. Simple {},
  119. #[route("/panic")]
  120. Panic {},
  121. #[route("/show")]
  122. Show {},
  123. }
  124. fn Home() -> Element {
  125. rsx! {
  126. ul {
  127. li {
  128. Link {
  129. to: Route::Simple {},
  130. "Simple errors"
  131. }
  132. }
  133. li {
  134. Link {
  135. to: Route::Panic {},
  136. "Capture panics"
  137. }
  138. }
  139. li {
  140. Link {
  141. to: Route::Show {},
  142. "Show errors"
  143. }
  144. }
  145. }
  146. }
  147. }