Merge pull request #2081 from ealmloff/unwind-into-error-boundary
unwind panics into error boundaries
This commit is contained in:
3 files changed
+33
-2
No files matched your search
@@ -20,6 +20,14 @@ fn app() -> Element {
|
|||||||
},
|
},
|
||||||
DemoC { x: 1 }
|
DemoC { x: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorBoundary {
|
||||||
|
handle_error: |error: CapturedError| rsx! {
|
||||||
|
h1 { "Another error occurred" }
|
||||||
|
pre { "{error:#?}" }
|
||||||
|
},
|
||||||
|
ComponentPanic {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,3 +48,7 @@ fn DemoC(x: i32) -> Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ComponentPanic() -> Element {
|
||||||
|
panic!("This component panics")
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
use crate::{nodes::RenderReturn, ComponentFunction};
|
use crate::{
|
||||||
|
innerlude::{throw_error, CapturedPanic},
|
||||||
|
nodes::RenderReturn,
|
||||||
|
ComponentFunction,
|
||||||
|
};
|
||||||
use std::{any::Any, panic::AssertUnwindSafe};
|
use std::{any::Any, panic::AssertUnwindSafe};
|
||||||
|
|
||||||
pub(crate) type BoxedAnyProps = Box<dyn AnyProps>;
|
pub(crate) type BoxedAnyProps = Box<dyn AnyProps>;
|
||||||
@@ -79,6 +83,7 @@ impl<F: ComponentFunction<P, M> + Clone, P: Clone + 'static, M: 'static> AnyProp
|
|||||||
Err(err) => {
|
Err(err) => {
|
||||||
let component_name = self.name;
|
let component_name = self.name;
|
||||||
tracing::error!("Error while rendering component `{component_name}`: {err:?}");
|
tracing::error!("Error while rendering component `{component_name}`: {err:?}");
|
||||||
|
throw_error::<()>(CapturedPanic { error: err });
|
||||||
RenderReturn::default()
|
RenderReturn::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,20 @@ use std::{
|
|||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A panic in a component that was caught by an error boundary.
|
||||||
|
///
|
||||||
|
/// NOTE: WASM currently does not support caching unwinds, so this struct will not be created in WASM.
|
||||||
|
pub struct CapturedPanic {
|
||||||
|
/// The error that was caught
|
||||||
|
pub error: Box<dyn Any + 'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for CapturedPanic {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("CapturedPanic").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Provide an error boundary to catch errors from child components
|
/// Provide an error boundary to catch errors from child components
|
||||||
pub fn use_error_boundary() -> ErrorBoundary {
|
pub fn use_error_boundary() -> ErrorBoundary {
|
||||||
use_hook(|| provide_context(ErrorBoundary::new()))
|
use_hook(|| provide_context(ErrorBoundary::new()))
|
||||||
@@ -201,7 +215,7 @@ pub trait Throw<S = ()>: Sized {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn throw_error<T>(e: impl Debug + 'static) -> Option<T> {
|
pub(crate) fn throw_error<T>(e: impl Debug + 'static) -> Option<T> {
|
||||||
if let Some(cx) = try_consume_context::<ErrorBoundary>() {
|
if let Some(cx) = try_consume_context::<ErrorBoundary>() {
|
||||||
match current_scope_id() {
|
match current_scope_id() {
|
||||||
Some(id) => cx.insert_error(id, Box::new(e), Backtrace::capture()),
|
Some(id) => cx.insert_error(id, Box::new(e), Backtrace::capture()),
|
||||||
|
|||||||
Reference in new issue
Block a user