1
0

error_boundary.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. use crate::{
  2. global_context::current_scope_id, innerlude::provide_context, use_hook, Element, IntoDynNode,
  3. Properties, ScopeId, Template, TemplateAttribute, TemplateNode, VNode,
  4. };
  5. use std::{
  6. any::Any,
  7. backtrace::Backtrace,
  8. cell::{Ref, RefCell},
  9. error::Error,
  10. fmt::{Debug, Display},
  11. rc::Rc,
  12. str::FromStr,
  13. };
  14. /// A panic in a component that was caught by an error boundary.
  15. ///
  16. /// <div class="warning">
  17. ///
  18. /// WASM currently does not support caching unwinds, so this struct will not be created in WASM.
  19. ///
  20. /// </div>
  21. pub struct CapturedPanic {
  22. #[allow(dead_code)]
  23. /// The error that was caught
  24. pub error: Box<dyn Any + 'static>,
  25. }
  26. impl Debug for CapturedPanic {
  27. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  28. f.debug_struct("CapturedPanic").finish()
  29. }
  30. }
  31. impl Display for CapturedPanic {
  32. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  33. f.write_fmt(format_args!("Encountered panic: {:?}", self.error))
  34. }
  35. }
  36. impl Error for CapturedPanic {}
  37. /// Provide an error boundary to catch errors from child components
  38. pub fn provide_error_boundary() -> ErrorContext {
  39. provide_context(ErrorContext::new(
  40. Vec::new(),
  41. current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
  42. ))
  43. }
  44. /// A trait for any type that can be downcast to a concrete type and implements Debug. This is automatically implemented for all types that implement Any + Debug.
  45. pub trait AnyError {
  46. fn as_any(&self) -> &dyn Any;
  47. fn as_error(&self) -> &dyn Error;
  48. }
  49. /// An wrapper error type for types that only implement Display. We use a inner type here to avoid overlapping implementations for DisplayError and impl Error
  50. struct DisplayError(DisplayErrorInner);
  51. impl<E: Display + 'static> From<E> for DisplayError {
  52. fn from(e: E) -> Self {
  53. Self(DisplayErrorInner(Box::new(e)))
  54. }
  55. }
  56. struct DisplayErrorInner(Box<dyn Display>);
  57. impl Display for DisplayErrorInner {
  58. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  59. self.0.fmt(f)
  60. }
  61. }
  62. impl Debug for DisplayErrorInner {
  63. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  64. self.0.fmt(f)
  65. }
  66. }
  67. impl Error for DisplayErrorInner {}
  68. impl AnyError for DisplayError {
  69. fn as_any(&self) -> &dyn Any {
  70. &self.0 .0
  71. }
  72. fn as_error(&self) -> &dyn Error {
  73. &self.0
  74. }
  75. }
  76. /// Provides context methods to [`Result`] and [`Option`] types that are compatible with [`CapturedError`]
  77. ///
  78. /// This trait is sealed and cannot be implemented outside of dioxus-core
  79. pub trait Context<T, E>: private::Sealed {
  80. /// Add a visual representation of the error that the [`ErrorBoundary`] may render
  81. ///
  82. /// # Example
  83. /// ```rust
  84. /// # use dioxus::prelude::*;
  85. /// fn Component() -> Element {
  86. /// // You can bubble up errors with `?` inside components, and event handlers
  87. /// // Along with the error itself, you can provide a way to display the error by calling `show`
  88. /// let number = "1234".parse::<usize>().show(|error| rsx! {
  89. /// div {
  90. /// background_color: "red",
  91. /// color: "white",
  92. /// "Error parsing number: {error}"
  93. /// }
  94. /// })?;
  95. /// unimplemented!()
  96. /// }
  97. /// ```
  98. fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T>;
  99. /// Wrap the result additional context about the error that occurred.
  100. ///
  101. /// # Example
  102. /// ```rust
  103. /// # use dioxus::prelude::*;
  104. /// fn NumberParser() -> Element {
  105. /// // You can bubble up errors with `?` inside components, and event handlers
  106. /// // Along with the error itself, you can provide a way to display the error by calling `context`
  107. /// let number = "-1234".parse::<usize>().context("Parsing number inside of the NumberParser")?;
  108. /// unimplemented!()
  109. /// }
  110. /// ```
  111. fn context<C: Display + 'static>(self, context: C) -> Result<T>;
  112. /// Wrap the result with additional context about the error that occurred. The closure will only be run if the Result is an error.
  113. ///
  114. /// # Example
  115. /// ```rust
  116. /// # use dioxus::prelude::*;
  117. /// fn NumberParser() -> Element {
  118. /// // You can bubble up errors with `?` inside components, and event handlers
  119. /// // Along with the error itself, you can provide a way to display the error by calling `context`
  120. /// let number = "-1234".parse::<usize>().with_context(|| format!("Timestamp: {:?}", std::time::Instant::now()))?;
  121. /// unimplemented!()
  122. /// }
  123. /// ```
  124. fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T>;
  125. }
  126. impl<T, E> Context<T, E> for std::result::Result<T, E>
  127. where
  128. E: Error + 'static,
  129. {
  130. fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T> {
  131. // We don't use result mapping to avoid extra frames
  132. match self {
  133. std::result::Result::Ok(value) => Ok(value),
  134. Err(error) => {
  135. let render = display_error(&error).unwrap_or_default();
  136. let mut error: CapturedError = error.into();
  137. error.render = render;
  138. Err(error)
  139. }
  140. }
  141. }
  142. fn context<C: Display + 'static>(self, context: C) -> Result<T> {
  143. self.with_context(|| context)
  144. }
  145. fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T> {
  146. // We don't use result mapping to avoid extra frames
  147. match self {
  148. std::result::Result::Ok(value) => Ok(value),
  149. Err(error) => {
  150. let mut error: CapturedError = error.into();
  151. error.context.push(Rc::new(AdditionalErrorContext {
  152. backtrace: Backtrace::capture(),
  153. context: Box::new(context()),
  154. scope: current_scope_id().ok(),
  155. }));
  156. Err(error)
  157. }
  158. }
  159. }
  160. }
  161. impl<T> Context<T, CapturedError> for Option<T> {
  162. fn show(self, display_error: impl FnOnce(&CapturedError) -> Element) -> Result<T> {
  163. // We don't use result mapping to avoid extra frames
  164. match self {
  165. Some(value) => Ok(value),
  166. None => {
  167. let mut error = CapturedError::from_display("Value was none");
  168. let render = display_error(&error).unwrap_or_default();
  169. error.render = render;
  170. Err(error)
  171. }
  172. }
  173. }
  174. fn context<C: Display + 'static>(self, context: C) -> Result<T> {
  175. self.with_context(|| context)
  176. }
  177. fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T> {
  178. // We don't use result mapping to avoid extra frames
  179. match self {
  180. Some(value) => Ok(value),
  181. None => {
  182. let error = CapturedError::from_display(context());
  183. Err(error)
  184. }
  185. }
  186. }
  187. }
  188. pub(crate) mod private {
  189. use super::*;
  190. pub trait Sealed {}
  191. impl<T, E> Sealed for std::result::Result<T, E> where E: Error {}
  192. impl<T> Sealed for Option<T> {}
  193. }
  194. impl<T: Any + Error> AnyError for T {
  195. fn as_any(&self) -> &dyn Any {
  196. self
  197. }
  198. fn as_error(&self) -> &dyn Error {
  199. self
  200. }
  201. }
  202. /// A context with information about suspended components
  203. #[derive(Debug, Clone)]
  204. pub struct ErrorContext {
  205. errors: Rc<RefCell<Vec<CapturedError>>>,
  206. id: ScopeId,
  207. }
  208. impl PartialEq for ErrorContext {
  209. fn eq(&self, other: &Self) -> bool {
  210. Rc::ptr_eq(&self.errors, &other.errors)
  211. }
  212. }
  213. impl ErrorContext {
  214. /// Create a new suspense boundary in a specific scope
  215. pub(crate) fn new(errors: Vec<CapturedError>, id: ScopeId) -> Self {
  216. Self {
  217. errors: Rc::new(RefCell::new(errors)),
  218. id,
  219. }
  220. }
  221. /// Get all errors thrown from child components
  222. pub fn errors(&self) -> Ref<[CapturedError]> {
  223. Ref::map(self.errors.borrow(), |errors| errors.as_slice())
  224. }
  225. /// Get the Element from the first error that can be shown
  226. pub fn show(&self) -> Option<Element> {
  227. self.errors.borrow().iter().find_map(|task| task.show())
  228. }
  229. /// Push an error into this Error Boundary
  230. pub fn insert_error(&self, error: CapturedError) {
  231. self.errors.borrow_mut().push(error);
  232. self.id.needs_update();
  233. }
  234. /// Clear all errors from this Error Boundary
  235. pub fn clear_errors(&self) {
  236. self.errors.borrow_mut().clear();
  237. self.id.needs_update();
  238. }
  239. }
  240. /// Errors can have additional context added as they bubble up the render tree
  241. /// This context can be used to provide additional information to the user
  242. struct AdditionalErrorContext {
  243. backtrace: Backtrace,
  244. context: Box<dyn Display>,
  245. scope: Option<ScopeId>,
  246. }
  247. impl Debug for AdditionalErrorContext {
  248. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  249. f.debug_struct("ErrorContext")
  250. .field("backtrace", &self.backtrace)
  251. .field("context", &self.context.to_string())
  252. .finish()
  253. }
  254. }
  255. impl Display for AdditionalErrorContext {
  256. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  257. let AdditionalErrorContext {
  258. backtrace,
  259. context,
  260. scope,
  261. } = self;
  262. write!(f, "{context} (from ")?;
  263. if let Some(scope) = scope {
  264. write!(f, "scope {scope:?} ")?;
  265. }
  266. write!(f, "at {backtrace:?})")
  267. }
  268. }
  269. /// A type alias for a result that can be either a boxed error or a value
  270. /// This is useful to avoid having to use `Result<T, CapturedError>` everywhere
  271. pub type Result<T = ()> = std::result::Result<T, CapturedError>;
  272. /// A helper function for an Ok result that can be either a boxed error or a value
  273. /// This is useful to avoid having to use `Ok<T, CapturedError>` everywhere
  274. #[allow(non_snake_case)]
  275. pub fn Ok<T>(value: T) -> Result<T> {
  276. Result::Ok(value)
  277. }
  278. #[derive(Clone)]
  279. /// An instance of an error captured by a descendant component.
  280. pub struct CapturedError {
  281. /// The error captured by the error boundary
  282. error: Rc<dyn AnyError + 'static>,
  283. /// The backtrace of the error
  284. backtrace: Rc<Backtrace>,
  285. /// The scope that threw the error
  286. scope: ScopeId,
  287. /// An error message that can be displayed to the user
  288. pub(crate) render: VNode,
  289. /// Additional context that was added to the error
  290. context: Vec<Rc<AdditionalErrorContext>>,
  291. }
  292. impl FromStr for CapturedError {
  293. type Err = std::convert::Infallible;
  294. fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
  295. std::result::Result::Ok(Self::from_display(s.to_string()))
  296. }
  297. }
  298. #[cfg(feature = "serialize")]
  299. #[derive(serde::Serialize, serde::Deserialize)]
  300. struct SerializedCapturedError {
  301. error: String,
  302. context: Vec<String>,
  303. }
  304. #[cfg(feature = "serialize")]
  305. impl serde::Serialize for CapturedError {
  306. fn serialize<S: serde::Serializer>(
  307. &self,
  308. serializer: S,
  309. ) -> std::result::Result<S::Ok, S::Error> {
  310. let serialized = SerializedCapturedError {
  311. error: self.error.as_error().to_string(),
  312. context: self
  313. .context
  314. .iter()
  315. .map(|context| context.to_string())
  316. .collect(),
  317. };
  318. serialized.serialize(serializer)
  319. }
  320. }
  321. #[cfg(feature = "serialize")]
  322. impl<'de> serde::Deserialize<'de> for CapturedError {
  323. fn deserialize<D: serde::Deserializer<'de>>(
  324. deserializer: D,
  325. ) -> std::result::Result<Self, D::Error> {
  326. let serialized = SerializedCapturedError::deserialize(deserializer)?;
  327. let error = DisplayError::from(serialized.error);
  328. let context = serialized
  329. .context
  330. .into_iter()
  331. .map(|context| {
  332. Rc::new(AdditionalErrorContext {
  333. scope: None,
  334. backtrace: Backtrace::disabled(),
  335. context: Box::new(context),
  336. })
  337. })
  338. .collect();
  339. std::result::Result::Ok(Self {
  340. error: Rc::new(error),
  341. context,
  342. backtrace: Rc::new(Backtrace::disabled()),
  343. scope: ScopeId::ROOT,
  344. render: VNode::placeholder(),
  345. })
  346. }
  347. }
  348. impl Debug for CapturedError {
  349. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  350. f.debug_struct("CapturedError")
  351. .field("error", &self.error.as_error())
  352. .field("backtrace", &self.backtrace)
  353. .field("scope", &self.scope)
  354. .finish()
  355. }
  356. }
  357. impl<E: AnyError + 'static> From<E> for CapturedError {
  358. fn from(error: E) -> Self {
  359. Self {
  360. error: Rc::new(error),
  361. backtrace: Rc::new(Backtrace::capture()),
  362. scope: current_scope_id()
  363. .expect("Cannot create an error boundary outside of a component's scope."),
  364. render: Default::default(),
  365. context: Default::default(),
  366. }
  367. }
  368. }
  369. impl CapturedError {
  370. /// Create a new captured error
  371. pub fn new(error: impl AnyError + 'static) -> Self {
  372. Self {
  373. error: Rc::new(error),
  374. backtrace: Rc::new(Backtrace::capture()),
  375. scope: current_scope_id().unwrap_or(ScopeId::ROOT),
  376. render: Default::default(),
  377. context: Default::default(),
  378. }
  379. }
  380. /// Create a new error from a type that only implements [`Display`]. If your type implements [`Error`], you can use [`CapturedError::from`] instead.
  381. pub fn from_display(error: impl Display + 'static) -> Self {
  382. Self {
  383. error: Rc::new(DisplayError::from(error)),
  384. backtrace: Rc::new(Backtrace::capture()),
  385. scope: current_scope_id().unwrap_or(ScopeId::ROOT),
  386. render: Default::default(),
  387. context: Default::default(),
  388. }
  389. }
  390. /// Mark the error as being thrown from a specific scope
  391. pub fn with_origin(mut self, scope: ScopeId) -> Self {
  392. self.scope = scope;
  393. self
  394. }
  395. /// Get a VNode representation of the error if the error provides one
  396. pub fn show(&self) -> Option<Element> {
  397. if self.render == VNode::placeholder() {
  398. None
  399. } else {
  400. Some(std::result::Result::Ok(self.render.clone()))
  401. }
  402. }
  403. /// Create a deep clone of this error
  404. pub(crate) fn deep_clone(&self) -> Self {
  405. Self {
  406. render: self.render.deep_clone(),
  407. ..self.clone()
  408. }
  409. }
  410. }
  411. impl PartialEq for CapturedError {
  412. fn eq(&self, other: &Self) -> bool {
  413. format!("{:?}", self) == format!("{:?}", other)
  414. }
  415. }
  416. impl Display for CapturedError {
  417. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  418. f.write_fmt(format_args!(
  419. "Encountered error: {:?}\nIn scope: {:?}\nBacktrace: {}\nContext: ",
  420. self.error.as_error(),
  421. self.scope,
  422. self.backtrace
  423. ))?;
  424. for context in &*self.context {
  425. f.write_fmt(format_args!("{}\n", context))?;
  426. }
  427. std::result::Result::Ok(())
  428. }
  429. }
  430. impl CapturedError {
  431. /// Downcast the error type into a concrete error type
  432. pub fn downcast<T: 'static>(&self) -> Option<&T> {
  433. self.error.as_any().downcast_ref::<T>()
  434. }
  435. }
  436. pub(crate) fn throw_into(error: impl Into<CapturedError>, scope: ScopeId) {
  437. let error = error.into();
  438. if let Some(cx) = scope.consume_context::<ErrorContext>() {
  439. cx.insert_error(error)
  440. } else {
  441. tracing::error!(
  442. "Tried to throw an error into an error boundary, but failed to locate a boundary: {:?}",
  443. error
  444. )
  445. }
  446. }
  447. #[allow(clippy::type_complexity)]
  448. #[derive(Clone)]
  449. pub struct ErrorHandler(Rc<dyn Fn(ErrorContext) -> Element>);
  450. impl<F: Fn(ErrorContext) -> Element + 'static> From<F> for ErrorHandler {
  451. fn from(value: F) -> Self {
  452. Self(Rc::new(value))
  453. }
  454. }
  455. fn default_handler(errors: ErrorContext) -> Element {
  456. static TEMPLATE: Template = Template {
  457. roots: &[TemplateNode::Element {
  458. tag: "div",
  459. namespace: None,
  460. attrs: &[TemplateAttribute::Static {
  461. name: "color",
  462. namespace: Some("style"),
  463. value: "red",
  464. }],
  465. children: &[TemplateNode::Dynamic { id: 0usize }],
  466. }],
  467. node_paths: &[&[0u8, 0u8]],
  468. attr_paths: &[],
  469. };
  470. std::result::Result::Ok(VNode::new(
  471. None,
  472. TEMPLATE,
  473. Box::new([errors
  474. .errors()
  475. .iter()
  476. .map(|e| {
  477. static TEMPLATE: Template = Template {
  478. roots: &[TemplateNode::Element {
  479. tag: "pre",
  480. namespace: None,
  481. attrs: &[],
  482. children: &[TemplateNode::Dynamic { id: 0usize }],
  483. }],
  484. node_paths: &[&[0u8, 0u8]],
  485. attr_paths: &[],
  486. };
  487. VNode::new(
  488. None,
  489. TEMPLATE,
  490. Box::new([e.to_string().into_dyn_node()]),
  491. Default::default(),
  492. )
  493. })
  494. .into_dyn_node()]),
  495. Default::default(),
  496. ))
  497. }
  498. #[derive(Clone)]
  499. pub struct ErrorBoundaryProps {
  500. children: Element,
  501. handle_error: ErrorHandler,
  502. }
  503. impl ErrorBoundaryProps {
  504. /**
  505. Create a builder for building `ErrorBoundaryProps`.
  506. On the builder, call `.children(...)`(optional), `.handle_error(...)`(optional) to set the values of the fields.
  507. Finally, call `.build()` to create the instance of `ErrorBoundaryProps`.
  508. */
  509. #[allow(dead_code)]
  510. pub fn builder() -> ErrorBoundaryPropsBuilder<((), ())> {
  511. ErrorBoundaryPropsBuilder { fields: ((), ()) }
  512. }
  513. }
  514. #[must_use]
  515. #[doc(hidden)]
  516. #[allow(dead_code, non_camel_case_types, non_snake_case)]
  517. pub struct ErrorBoundaryPropsBuilder<TypedBuilderFields> {
  518. fields: TypedBuilderFields,
  519. }
  520. impl<TypedBuilderFields> Clone for ErrorBoundaryPropsBuilder<TypedBuilderFields>
  521. where
  522. TypedBuilderFields: Clone,
  523. {
  524. fn clone(&self) -> Self {
  525. Self {
  526. fields: self.fields.clone(),
  527. }
  528. }
  529. }
  530. impl Properties for ErrorBoundaryProps {
  531. type Builder = ErrorBoundaryPropsBuilder<((), ())>;
  532. fn builder() -> Self::Builder {
  533. ErrorBoundaryProps::builder()
  534. }
  535. fn memoize(&mut self, other: &Self) -> bool {
  536. *self = other.clone();
  537. false
  538. }
  539. }
  540. #[doc(hidden)]
  541. #[allow(dead_code, non_camel_case_types, non_snake_case)]
  542. pub trait ErrorBoundaryPropsBuilder_Optional<T> {
  543. fn into_value<F: FnOnce() -> T>(self, default: F) -> T;
  544. }
  545. impl<T> ErrorBoundaryPropsBuilder_Optional<T> for () {
  546. fn into_value<F: FnOnce() -> T>(self, default: F) -> T {
  547. default()
  548. }
  549. }
  550. impl<T> ErrorBoundaryPropsBuilder_Optional<T> for (T,) {
  551. fn into_value<F: FnOnce() -> T>(self, _: F) -> T {
  552. self.0
  553. }
  554. }
  555. #[allow(dead_code, non_camel_case_types, missing_docs)]
  556. impl<__handle_error> ErrorBoundaryPropsBuilder<((), __handle_error)> {
  557. pub fn children(
  558. self,
  559. children: Element,
  560. ) -> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
  561. let children = (children,);
  562. let (_, handle_error) = self.fields;
  563. ErrorBoundaryPropsBuilder {
  564. fields: (children, handle_error),
  565. }
  566. }
  567. }
  568. #[doc(hidden)]
  569. #[allow(dead_code, non_camel_case_types, non_snake_case)]
  570. pub enum ErrorBoundaryPropsBuilder_Error_Repeated_field_children {}
  571. #[doc(hidden)]
  572. #[allow(dead_code, non_camel_case_types, missing_docs)]
  573. impl<__handle_error> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
  574. #[deprecated(note = "Repeated field children")]
  575. pub fn children(
  576. self,
  577. _: ErrorBoundaryPropsBuilder_Error_Repeated_field_children,
  578. ) -> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
  579. self
  580. }
  581. }
  582. #[allow(dead_code, non_camel_case_types, missing_docs)]
  583. impl<__children> ErrorBoundaryPropsBuilder<(__children, ())> {
  584. pub fn handle_error(
  585. self,
  586. handle_error: impl ::core::convert::Into<ErrorHandler>,
  587. ) -> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
  588. let handle_error = (handle_error.into(),);
  589. let (children, _) = self.fields;
  590. ErrorBoundaryPropsBuilder {
  591. fields: (children, handle_error),
  592. }
  593. }
  594. }
  595. #[doc(hidden)]
  596. #[allow(dead_code, non_camel_case_types, non_snake_case)]
  597. pub enum ErrorBoundaryPropsBuilder_Error_Repeated_field_handle_error {}
  598. #[doc(hidden)]
  599. #[allow(dead_code, non_camel_case_types, missing_docs)]
  600. impl<__children> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
  601. #[deprecated(note = "Repeated field handle_error")]
  602. pub fn handle_error(
  603. self,
  604. _: ErrorBoundaryPropsBuilder_Error_Repeated_field_handle_error,
  605. ) -> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
  606. self
  607. }
  608. }
  609. #[allow(dead_code, non_camel_case_types, missing_docs)]
  610. impl<
  611. __handle_error: ErrorBoundaryPropsBuilder_Optional<ErrorHandler>,
  612. __children: ErrorBoundaryPropsBuilder_Optional<Element>,
  613. > ErrorBoundaryPropsBuilder<(__children, __handle_error)>
  614. {
  615. pub fn build(self) -> ErrorBoundaryProps {
  616. let (children, handle_error) = self.fields;
  617. let children = ErrorBoundaryPropsBuilder_Optional::into_value(children, VNode::empty);
  618. let handle_error = ErrorBoundaryPropsBuilder_Optional::into_value(handle_error, || {
  619. ErrorHandler(Rc::new(default_handler))
  620. });
  621. ErrorBoundaryProps {
  622. children,
  623. handle_error,
  624. }
  625. }
  626. }
  627. /// Create a new error boundary component that catches any errors thrown from child components
  628. ///
  629. /// ## Details
  630. ///
  631. /// Error boundaries handle errors within a specific part of your application. They are similar to `try/catch` in JavaScript, but they only catch errors in the tree below them.
  632. /// Any errors passed up from a child will be caught by the nearest error boundary. Error boundaries are quick to implement, but it can be useful to individually handle errors
  633. /// in your components to provide a better user experience when you know that an error is likely to occur.
  634. ///
  635. /// ## Example
  636. ///
  637. /// ```rust, no_run
  638. /// use dioxus::prelude::*;
  639. ///
  640. /// fn App() -> Element {
  641. /// let mut multiplier = use_signal(|| String::from("2"));
  642. /// rsx! {
  643. /// input {
  644. /// r#type: "text",
  645. /// value: multiplier,
  646. /// oninput: move |e| multiplier.set(e.value())
  647. /// }
  648. /// ErrorBoundary {
  649. /// handle_error: |errors: ErrorContext| {
  650. /// rsx! {
  651. /// div {
  652. /// "Oops, we encountered an error. Please report {errors:?} to the developer of this application"
  653. /// }
  654. /// }
  655. /// },
  656. /// Counter {
  657. /// multiplier
  658. /// }
  659. /// }
  660. /// }
  661. /// }
  662. ///
  663. /// #[component]
  664. /// fn Counter(multiplier: ReadOnlySignal<String>) -> Element {
  665. /// let multiplier_parsed = multiplier().parse::<usize>()?;
  666. /// let mut count = use_signal(|| multiplier_parsed);
  667. /// rsx! {
  668. /// button {
  669. /// onclick: move |_| {
  670. /// let multiplier_parsed = multiplier().parse::<usize>()?;
  671. /// *count.write() *= multiplier_parsed;
  672. /// Ok(())
  673. /// },
  674. /// "{count}x{multiplier}"
  675. /// }
  676. /// }
  677. /// }
  678. /// ```
  679. ///
  680. /// ## Resetting the error boundary
  681. ///
  682. /// Once the error boundary catches an error, it will render the rsx returned from the handle_error function instead of the children. To reset the error boundary,
  683. /// you can call the [`ErrorContext::clear_errors`] method. This will clear all errors and re-render the children.
  684. ///
  685. /// ```rust, no_run
  686. /// # use dioxus::prelude::*;
  687. /// fn App() -> Element {
  688. /// let mut multiplier = use_signal(|| String::new());
  689. /// rsx! {
  690. /// input {
  691. /// r#type: "text",
  692. /// value: multiplier,
  693. /// oninput: move |e| multiplier.set(e.value())
  694. /// }
  695. /// ErrorBoundary {
  696. /// handle_error: |errors: ErrorContext| {
  697. /// rsx! {
  698. /// div {
  699. /// "Oops, we encountered an error. Please report {errors:?} to the developer of this application"
  700. /// }
  701. /// button {
  702. /// onclick: move |_| {
  703. /// errors.clear_errors();
  704. /// },
  705. /// "try again"
  706. /// }
  707. /// }
  708. /// },
  709. /// Counter {
  710. /// multiplier
  711. /// }
  712. /// }
  713. /// }
  714. /// }
  715. ///
  716. /// #[component]
  717. /// fn Counter(multiplier: ReadOnlySignal<String>) -> Element {
  718. /// let multiplier_parsed = multiplier().parse::<usize>()?;
  719. /// let mut count = use_signal(|| multiplier_parsed);
  720. /// rsx! {
  721. /// button {
  722. /// onclick: move |_| {
  723. /// let multiplier_parsed = multiplier().parse::<usize>()?;
  724. /// *count.write() *= multiplier_parsed;
  725. /// Ok(())
  726. /// },
  727. /// "{count}x{multiplier}"
  728. /// }
  729. /// }
  730. /// }
  731. /// ```
  732. #[allow(non_upper_case_globals, non_snake_case)]
  733. pub fn ErrorBoundary(props: ErrorBoundaryProps) -> Element {
  734. let error_boundary = use_hook(provide_error_boundary);
  735. let errors = error_boundary.errors();
  736. let has_errors = !errors.is_empty();
  737. // Drop errors before running user code that might borrow the error lock
  738. drop(errors);
  739. if has_errors {
  740. (props.handle_error.0)(error_boundary.clone())
  741. } else {
  742. std::result::Result::Ok({
  743. static TEMPLATE: Template = Template {
  744. roots: &[TemplateNode::Dynamic { id: 0usize }],
  745. node_paths: &[&[0u8]],
  746. attr_paths: &[],
  747. };
  748. VNode::new(
  749. None,
  750. TEMPLATE,
  751. Box::new([(props.children).into_dyn_node()]),
  752. Default::default(),
  753. )
  754. })
  755. }
  756. }