Skip to content

Commit 9356fa1

Browse files
authored
feat(core): include type name in state panic message (#13239)
* feat(core): include type name in state panic message * tweak message
1 parent 2dccfab commit 9356fa1

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Enhance panic message when fetching unmanaged state.

crates/tauri/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,12 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
738738
where
739739
T: Send + Sync + 'static,
740740
{
741-
self
742-
.manager()
743-
.state
744-
.try_get()
745-
.expect("state() called before manage() for given type")
741+
self.manager().state.try_get().unwrap_or_else(|| {
742+
panic!(
743+
"state() called before manage() for {}",
744+
std::any::type_name::<T>()
745+
)
746+
})
746747
}
747748

748749
/// Attempts to retrieve the managed state for the type `T`.

crates/tauri/src/state.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl StateManager {
153153
pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
154154
self
155155
.try_get()
156-
.expect("state: get() when given type is not managed")
156+
.unwrap_or_else(|| panic!("state not found for type {}", std::any::type_name::<T>()))
157157
}
158158

159159
/// Gets the state associated with the specified type.
@@ -191,6 +191,13 @@ mod tests {
191191
}
192192
}
193193

194+
#[test]
195+
#[should_panic(expected = "state not found for type core::option::Option<alloc::string::String>")]
196+
fn get_panics() {
197+
let state = StateManager::new();
198+
state.get::<Option<String>>();
199+
}
200+
194201
#[test]
195202
fn simple_set_get() {
196203
let state = StateManager::new();

0 commit comments

Comments
 (0)