Follow-up for error messages in core (#2719)

* Fix tests

* Replace unwraps on RuntimeError with panics and add track_caller for better error output

* Convert more unwraps

* Fix git
This commit is contained in:
Matt Hunzinger authored and GitHub committed 2024-07-29 13:15:06 -07:00
1 parent a1dfc4b4f0
commit 6558fd95a2
9 files changed
+39 -17

No files matched your search

+6 -1
View File
@@ -41,7 +41,12 @@ impl Error for CapturedPanic {}
/// 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() -> ErrorContext { pub fn use_error_boundary() -> ErrorContext {
use_hook(|| provide_context(ErrorContext::new(Vec::new(), current_scope_id().unwrap()))) use_hook(|| {
provide_context(ErrorContext::new(
Vec::new(),
current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
))
})
} }
/// 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. /// 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.
+4 -3
View File
@@ -397,7 +397,7 @@ impl<Args: 'static, Ret: 'static> Callback<Args, Ret> {
)); ));
Self { Self {
callback, callback,
origin: current_scope_id().unwrap(), origin: current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
} }
} }
@@ -409,13 +409,14 @@ impl<Args: 'static, Ret: 'static> Callback<Args, Ret> {
as Rc<RefCell<dyn FnMut(Args) -> Ret>>)); as Rc<RefCell<dyn FnMut(Args) -> Ret>>));
Self { Self {
callback, callback,
origin: current_scope_id().unwrap(), origin: current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
} }
} }
/// Call this callback with the appropriate argument type /// Call this callback with the appropriate argument type
/// ///
/// This borrows the callback using a RefCell. Recursively calling a callback will cause a panic. /// This borrows the callback using a RefCell. Recursively calling a callback will cause a panic.
#[track_caller]
pub fn call(&self, arguments: Args) -> Ret { pub fn call(&self, arguments: Args) -> Ret {
if let Some(callback) = self.callback.read().as_ref() { if let Some(callback) = self.callback.read().as_ref() {
Runtime::with(|rt| { Runtime::with(|rt| {
@@ -427,7 +428,7 @@ impl<Args: 'static, Ret: 'static> Callback<Args, Ret> {
value value
}) })
}) })
.unwrap() .unwrap_or_else(|e| panic!("{}", e))
} else { } else {
panic!("Callback was manually dropped") panic!("Callback was manually dropped")
} }
+2 -1
View File
@@ -90,7 +90,8 @@ pub fn current_owner<S: AnyStorage>() -> Owner<S> {
impl ScopeId { impl ScopeId {
/// Get the owner for the current scope. /// Get the owner for the current scope.
#[track_caller]
pub fn owner<S: AnyStorage>(self) -> Owner<S> { pub fn owner<S: AnyStorage>(self) -> Owner<S> {
Runtime::with_scope(self, |cx| cx.owner::<S>()).unwrap() Runtime::with_scope(self, |cx| cx.owner::<S>()).unwrap_or_else(|e| panic!("{}", e))
} }
} }
+7 -3
View File
@@ -35,7 +35,9 @@ pub fn vdom_is_rendering() -> bool {
/// } /// }
/// ``` /// ```
pub fn throw_error(error: impl Into<CapturedError> + 'static) { pub fn throw_error(error: impl Into<CapturedError> + 'static) {
current_scope_id().unwrap().throw_error(error) current_scope_id()
.unwrap_or_else(|e| panic!("{}", e))
.throw_error(error)
} }
/// Consume context from the current scope /// Consume context from the current scope
@@ -289,8 +291,9 @@ pub fn needs_update_any(id: ScopeId) {
/// Note: Unlike [`needs_update`], the function returned by this method will work outside of the dioxus runtime. /// Note: Unlike [`needs_update`], the function returned by this method will work outside of the dioxus runtime.
/// ///
/// You should prefer [`schedule_update_any`] if you need to update multiple components. /// You should prefer [`schedule_update_any`] if you need to update multiple components.
#[track_caller]
pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> { pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> {
Runtime::with_current_scope(|cx| cx.schedule_update()).unwrap() Runtime::with_current_scope(|cx| cx.schedule_update()).unwrap_or_else(|e| panic!("{}", e))
} }
/// Schedule an update for any component given its [`ScopeId`]. /// Schedule an update for any component given its [`ScopeId`].
@@ -298,8 +301,9 @@ pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> {
/// A component's [`ScopeId`] can be obtained from the [`current_scope_id`] method. /// A component's [`ScopeId`] can be obtained from the [`current_scope_id`] method.
/// ///
/// Note: Unlike [`needs_update`], the function returned by this method will work outside of the dioxus runtime. /// Note: Unlike [`needs_update`], the function returned by this method will work outside of the dioxus runtime.
#[track_caller]
pub fn schedule_update_any() -> Arc<dyn Fn(ScopeId) + Send + Sync> { pub fn schedule_update_any() -> Arc<dyn Fn(ScopeId) + Send + Sync> {
Runtime::with_current_scope(|cx| cx.schedule_update_any()).unwrap() Runtime::with_current_scope(|cx| cx.schedule_update_any()).unwrap_or_else(|e| panic!("{}", e))
} }
/// Creates a callback that will be run before the component is removed. /// Creates a callback that will be run before the component is removed.
+5 -1
View File
@@ -67,7 +67,11 @@ impl ReactiveContext {
} }
let _ = tx.unbounded_send(()); let _ = tx.unbounded_send(());
}; };
let _self = Self::new_with_callback(callback, current_scope_id().unwrap(), origin); let _self = Self::new_with_callback(
callback,
current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
origin,
);
(_self, rx) (_self, rx)
} }
+1 -1
View File
@@ -50,7 +50,7 @@ impl VirtualDom {
#[track_caller] #[track_caller]
pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> RenderReturn { pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> RenderReturn {
// Ensure we are currently inside a `Runtime`. // Ensure we are currently inside a `Runtime`.
crate::Runtime::current().unwrap(); crate::Runtime::current().unwrap_or_else(|e| panic!("{}", e));
self.runtime.clone().with_scope_on_stack(scope_id, || { self.runtime.clone().with_scope_on_stack(scope_id, || {
let scope = &self.scopes[scope_id.0]; let scope = &self.scopes[scope_id.0];
+4 -2
View File
@@ -97,7 +97,7 @@ impl Scope {
} }
fn sender(&self) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> { fn sender(&self) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> {
Runtime::with(|rt| rt.sender.clone()).unwrap() Runtime::with(|rt| rt.sender.clone()).unwrap_or_else(|e| panic!("{}", e))
} }
/// Mount the scope and queue any pending effects if it is not already mounted /// Mount the scope and queue any pending effects if it is not already mounted
@@ -531,7 +531,9 @@ impl ScopeId {
/// Run a closure inside of scope's runtime /// Run a closure inside of scope's runtime
#[track_caller] #[track_caller]
pub fn in_runtime<T>(self, f: impl FnOnce() -> T) -> T { pub fn in_runtime<T>(self, f: impl FnOnce() -> T) -> T {
Runtime::current().unwrap().on_scope(self, f) Runtime::current()
.unwrap_or_else(|e| panic!("{}", e))
.on_scope(self, f)
} }
/// Throw a [`CapturedError`] into a scope. The error will bubble up to the nearest [`ErrorBoundary`] or the root of the app. /// Throw a [`CapturedError`] into a scope. The error will bubble up to the nearest [`ErrorBoundary`] or the root of the app.
+1 -1
View File
@@ -46,7 +46,7 @@ impl SuspendedFuture {
pub fn new(task: Task) -> Self { pub fn new(task: Task) -> Self {
Self { Self {
task, task,
origin: current_scope_id().unwrap(), origin: current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
placeholder: VNode::placeholder(), placeholder: VNode::placeholder(),
} }
} }
+9 -4
View File
@@ -7,6 +7,7 @@ use crate::ScopeId;
use futures_util::task::ArcWake; use futures_util::task::ArcWake;
use slotmap::DefaultKey; use slotmap::DefaultKey;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::panic;
use std::sync::Arc; use std::sync::Arc;
use std::task::Waker; use std::task::Waker;
use std::{cell::Cell, future::Future}; use std::{cell::Cell, future::Future};
@@ -76,21 +77,24 @@ impl Task {
} }
/// Wake the task. /// Wake the task.
#[track_caller]
pub fn wake(&self) { pub fn wake(&self) {
Runtime::with(|rt| { Runtime::with(|rt| {
_ = rt _ = rt
.sender .sender
.unbounded_send(SchedulerMsg::TaskNotified(self.id)) .unbounded_send(SchedulerMsg::TaskNotified(self.id))
}) })
.unwrap(); .unwrap_or_else(|e| panic!("{}", e))
} }
/// Poll the task immediately. /// Poll the task immediately.
#[track_caller]
pub fn poll_now(&self) -> Poll<()> { pub fn poll_now(&self) -> Poll<()> {
Runtime::with(|rt| rt.handle_task_wakeup(*self)).unwrap() Runtime::with(|rt| rt.handle_task_wakeup(*self)).unwrap_or_else(|e| panic!("{}", e))
} }
/// Set the task as active or paused. /// Set the task as active or paused.
#[track_caller]
pub fn set_active(&self, active: bool) { pub fn set_active(&self, active: bool) {
Runtime::with(|rt| { Runtime::with(|rt| {
if let Some(task) = rt.tasks.borrow().get(self.id) { if let Some(task) = rt.tasks.borrow().get(self.id) {
@@ -102,7 +106,7 @@ impl Task {
} }
} }
}) })
.unwrap(); .unwrap_or_else(|e| panic!("{}", e))
} }
} }
@@ -247,11 +251,12 @@ impl Runtime {
self.tasks.borrow().get(task.id).map(|t| t.scope) self.tasks.borrow().get(task.id).map(|t| t.scope)
} }
#[track_caller]
pub(crate) fn handle_task_wakeup(&self, id: Task) -> Poll<()> { pub(crate) fn handle_task_wakeup(&self, id: Task) -> Poll<()> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
// Ensure we are currently inside a `Runtime`. // Ensure we are currently inside a `Runtime`.
Runtime::current().unwrap(); Runtime::current().unwrap_or_else(|e| panic!("{}", e));
} }
let task = self.tasks.borrow().get(id.id).cloned(); let task = self.tasks.borrow().get(id.id).cloned();