diff --git a/examples/error_handle.rs b/examples/error_handle.rs index ccafe0642..5c3279940 100644 --- a/examples/error_handle.rs +++ b/examples/error_handle.rs @@ -20,6 +20,14 @@ fn app() -> Element { }, DemoC { x: 1 } } + + ErrorBoundary { + handle_error: |error: CapturedError| rsx! { + h1 { "Another error occurred" } + pre { "{error:#?}" } + }, + ComponentPanic {} + } } } @@ -40,3 +48,8 @@ fn DemoC(x: i32) -> Element { } } } + +#[component] +fn ComponentPanic() -> Element { + panic!("This component panics") +} diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index 9a2c0ebb5..638572fda 100644 --- a/packages/cli/src/builder.rs +++ b/packages/cli/src/builder.rs @@ -335,12 +335,6 @@ pub fn build_desktop( let _manganis_support = ManganisSupportGuard::default(); let _guard = AssetConfigDropGuard::new(); - // set the asset dir via cli args - env::set_var( - "DIOXUS_ASSET_DIR", - config.asset_dir().canonicalize().unwrap(), - ); - let mut cmd = subprocess::Exec::cmd("cargo") .set_rust_flags(rust_flags) .env("CARGO_TARGET_DIR", &config.target_dir) diff --git a/packages/core/src/any_props.rs b/packages/core/src/any_props.rs index b737a549a..760b821dc 100644 --- a/packages/core/src/any_props.rs +++ b/packages/core/src/any_props.rs @@ -1,4 +1,8 @@ -use crate::{nodes::RenderReturn, ComponentFunction}; +use crate::{ + innerlude::{throw_error, CapturedPanic}, + nodes::RenderReturn, + ComponentFunction, +}; use std::{any::Any, panic::AssertUnwindSafe}; pub(crate) type BoxedAnyProps = Box; @@ -79,6 +83,7 @@ impl + Clone, P: Clone + 'static, M: 'static> AnyProp Err(err) => { let component_name = self.name; tracing::error!("Error while rendering component `{component_name}`: {err:?}"); + throw_error::<()>(CapturedPanic { error: err }); RenderReturn::default() } } diff --git a/packages/core/src/error_boundary.rs b/packages/core/src/error_boundary.rs index 6e1055c6e..88359dd1c 100644 --- a/packages/core/src/error_boundary.rs +++ b/packages/core/src/error_boundary.rs @@ -13,6 +13,20 @@ use std::{ 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, +} + +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 pub fn use_error_boundary() -> ErrorBoundary { use_hook(|| provide_context(ErrorBoundary::new())) @@ -201,7 +215,7 @@ pub trait Throw: Sized { } } -fn throw_error(e: impl Debug + 'static) -> Option { +pub(crate) fn throw_error(e: impl Debug + 'static) -> Option { if let Some(cx) = try_consume_context::() { match current_scope_id() { Some(id) => cx.insert_error(id, Box::new(e), Backtrace::capture()), diff --git a/packages/hot-reload/src/file_watcher.rs b/packages/hot-reload/src/file_watcher.rs index 04f5e4369..ce4a7c8d8 100644 --- a/packages/hot-reload/src/file_watcher.rs +++ b/packages/hot-reload/src/file_watcher.rs @@ -243,7 +243,7 @@ pub fn init(cfg: Config) { // FIXME: use a more robust system here for embedded discovery // // https://github.com/DioxusLabs/dioxus/issues/1914 - if listening_paths == &[""] { + if listening_paths == [""] { for entry in std::fs::read_dir(&crate_dir) .expect("failed to read rust crate directory. Are you running with cargo?") {