{"record":{"id":"f14f2178b66059fe","repo":"tauri-apps/tauri","slug":"state-not-found-for-type","errorCode":null,"errorMessage":"state not found for type {}","messagePattern":"state not found for type (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/tauri/src/state.rs","lineNumber":145,"sourceCode":"  }\n\n  /// SAFETY: Calling this method will move the `value`,\n  /// which will cause references obtained through [Self::try_get] to dangle.\n  pub(crate) unsafe fn unmanage<T: Send + Sync + 'static>(&self) -> Option<T> {\n    let mut map = self.map.lock().unwrap();\n    let type_id = TypeId::of::<T>();\n    let state = map.remove(&type_id)?;\n    let value = state\n      .downcast::<T>()\n      .expect(\"the type of the key should be same as the type of the value\");\n    Some(*value)\n  }\n\n  /// Gets the state associated with the specified type.\n  pub fn get<T: 'static>(&self) -> State<'_, T> {\n    self\n      .try_get()\n      .unwrap_or_else(|| panic!(\"state not found for type {}\", std::any::type_name::<T>()))\n  }\n\n  /// Gets the state associated with the specified type.\n  pub fn try_get<T: 'static>(&self) -> Option<State<'_, T>> {\n    let map = self.map.lock().unwrap();\n    let type_id = TypeId::of::<T>();\n    let state = map.get(&type_id)?;\n    let value = state\n      .downcast_ref::<T>()\n      .expect(\"the type of the key should be same as the type of the value\");\n    // SAFETY: We ensure the lifetime of `value` is the same as [StateManager] and `value` will not be mutated/moved.\n    let v_ref = unsafe { &*(value as *const T) };\n    Some(State(v_ref))\n  }\n}\n\n// Ported from https://github.com/SergioBenitez/state/blob/556c1b94db8ce8427a0e72de7983ab5a9af4cc41/tests/main.rs\n#[cfg(test)]","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/tauri-apps/tauri/blob/460ec35447493200d64290dd7f015d5a91d0fd58/crates/tauri/src/state.rs#L127-L163","documentation":"State<'_, T>::get retrieves managed state of type T registered via Manager::manage. If no state of that type was ever managed on the app, try_get returns None and get panics with the type name. It is an unrecoverable programmer error: state must be registered before the app runs the event loop.","triggerScenarios":"Calling app.state::<MyState>() or window.state::<T>() when Manager::manage(MyState) / manage was never called, or called after .run() started, or on a different T than what was managed.","commonSituations":"Forgetting to call .manage(MyState::default()) in setup or before run; managing a different generic instantiation (e.g. HashMap<String, i32> vs HashMap<i32, i32>); accessing state in a plugin before it is initialized; tests spawning the app without setup.","solutions":["Register the state before running: app.manage(MyState::default())","Use try_get::<T>() to handle absence gracefully instead of panicking","Verify the exact type T matches the one passed to manage (generics are compared by TypeId)"],"exampleFix":"// before\nlet state = app.state::<Db>();\n// after\nfn main() -> tauri::Result<()> {\n  tauri::Builder::default()\n    .setup(|app| { app.manage(Db::connect()?); Ok(()) })\n    .run(tauri::generate_context!())\n}","handlingStrategy":"validation","validationCode":"if app.try_state::<MyState>().is_none() {\n  eprintln!(\"MyState not managed; call app.manage(MyState::default()) before run\");\n}","typeGuard":"fn has_state<T: Send + Sync + 'static>(app: &tauri::AppHandle) -> bool {\n  app.try_state::<T>().is_some()\n}","tryCatchPattern":"std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| app.state::<MyState>()))\n  .map_err(|_| anyhow::anyhow!(\"state <MyState> not managed\"))","preventionTips":["Register all state in .setup() or before .run()","Use try_state where absence is plausible","Match manage() types exactly (watch generics)"],"tags":["state-management","panic","rust"],"backgroundTag":"resource-not-found","analyzedSha":"460ec35447493200d64290dd7f015d5a91d0fd58","analyzedAt":"2026-09-18T23:55:51.277Z","contentChangedAt":"2026-09-18T23:55:51.277Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}