Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/enhance-state-panic-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Enhance panic message when fetching unmanaged state.
11 changes: 6 additions & 5 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,12 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
where
T: Send + Sync + 'static,
{
self
.manager()
.state
.try_get()
.expect("state() called before manage() for given type")
self.manager().state.try_get().unwrap_or_else(|| {
panic!(
"state() called before manage() for {}",
std::any::type_name::<T>()
)
})
}

/// Attempts to retrieve the managed state for the type `T`.
Expand Down
9 changes: 8 additions & 1 deletion crates/tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl StateManager {
pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
self
.try_get()
.expect("state: get() when given type is not managed")
.unwrap_or_else(|| panic!("state not found for type {}", std::any::type_name::<T>()))
}

/// Gets the state associated with the specified type.
Expand Down Expand Up @@ -191,6 +191,13 @@ mod tests {
}
}

#[test]
#[should_panic(expected = "state not found for type core::option::Option<alloc::string::String>")]
fn get_panics() {
let state = StateManager::new();
state.get::<Option<String>>();
}

#[test]
fn simple_set_get() {
let state = StateManager::new();
Expand Down