error_boundary.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #![allow(non_snake_case)]
  2. use dioxus::{prelude::*, CapturedError};
  3. #[test]
  4. fn catches_panic() {
  5. let mut dom = VirtualDom::new(app);
  6. dom.rebuild(&mut dioxus_core::NoOpMutations);
  7. }
  8. fn app() -> Element {
  9. rsx! {
  10. div {
  11. h1 { "Title" }
  12. NoneChild {}
  13. ThrowChild {}
  14. }
  15. }
  16. }
  17. fn NoneChild() -> Element {
  18. VNode::empty()
  19. }
  20. fn ThrowChild() -> Element {
  21. Err(std::io::Error::new(std::io::ErrorKind::AddrInUse, "asd"))?;
  22. let _g: i32 = "123123".parse()?;
  23. rsx! { div {} }
  24. }
  25. #[test]
  26. fn clear_error_boundary() {
  27. static THREW_ERROR: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
  28. #[component]
  29. fn App() -> Element {
  30. rsx! {
  31. AutoClearError {}
  32. }
  33. }
  34. #[component]
  35. pub fn ThrowsError() -> Element {
  36. if THREW_ERROR.load(std::sync::atomic::Ordering::SeqCst) {
  37. THREW_ERROR.store(true, std::sync::atomic::Ordering::SeqCst);
  38. Err(CapturedError::from_display("This is an error").into())
  39. } else {
  40. rsx! {
  41. "We should see this"
  42. }
  43. }
  44. }
  45. #[component]
  46. pub fn AutoClearError() -> Element {
  47. rsx! {
  48. ErrorBoundary {
  49. handle_error: |error: ErrorContext| {
  50. error.clear_errors();
  51. rsx! {
  52. "We cleared it"
  53. }
  54. },
  55. ThrowsError {}
  56. }
  57. }
  58. }
  59. let mut dom = VirtualDom::new(App);
  60. dom.rebuild(&mut dioxus_core::NoOpMutations);
  61. let out = dioxus_ssr::render(&dom);
  62. assert_eq!(out, "We should see this");
  63. }