dioxus_core/
error_boundary.rs

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