diff --git a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs index 571b050a1d877..507156c0fc997 100644 --- a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs @@ -87,7 +87,11 @@ impl LogDiagnosticsPlugin { ) { if state.timer.tick(time.delta()).finished() { if let Some(ref filter) = state.filter { - for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) { + for diagnostic in filter.iter().map(|id| { + diagnostics + .get(*id) + .unwrap_or_else(|| panic!("Could not find diagnostic with id {:?}", id)) + }) { Self::log_diagnostic(diagnostic); } } else { @@ -105,7 +109,11 @@ impl LogDiagnosticsPlugin { ) { if state.timer.tick(time.delta()).finished() { if let Some(ref filter) = state.filter { - for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) { + for diagnostic in filter.iter().map(|id| { + diagnostics + .get(*id) + .unwrap_or_else(|| panic!("Could not find diagnostic with id {:?}", id)) + }) { debug!("{:#?}\n", diagnostic); } } else { diff --git a/crates/bevy_scene/src/command.rs b/crates/bevy_scene/src/command.rs index aef43a2e7f0d0..87e0e4be79d8a 100644 --- a/crates/bevy_scene/src/command.rs +++ b/crates/bevy_scene/src/command.rs @@ -14,7 +14,9 @@ pub struct SpawnScene { impl Command for SpawnScene { fn write(self, world: &mut World) { - let mut spawner = world.get_resource_mut::().unwrap(); + let mut spawner = world + .get_resource_mut::() + .expect("Could not get `SceneSpawner` resource in the `World`. Did you forget to add `ScenePlugin` to your `App`?"); spawner.spawn(self.scene_handle); } } @@ -36,7 +38,9 @@ pub struct SpawnSceneAsChild { impl Command for SpawnSceneAsChild { fn write(self, world: &mut World) { - let mut spawner = world.get_resource_mut::().unwrap(); + let mut spawner = world + .get_resource_mut::() + .expect("Could not get `SceneSpawner` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?"); spawner.spawn_as_child(self.scene_handle, self.parent); } } diff --git a/crates/bevy_scene/src/dynamic_scene.rs b/crates/bevy_scene/src/dynamic_scene.rs index ad0d36fb0d0c0..fb1e01193ad76 100644 --- a/crates/bevy_scene/src/dynamic_scene.rs +++ b/crates/bevy_scene/src/dynamic_scene.rs @@ -52,7 +52,11 @@ impl DynamicScene { let reflect_component = world .components() .get_info(component_id) - .and_then(|info| type_registry.get(info.type_id().unwrap())) + .and_then(|info| { + type_registry.get(info.type_id().unwrap_or_else(|| { + panic!("Component {:?} has no `type_id`", info.name()) + })) + }) .and_then(|registration| registration.data::()); if let Some(reflect_component) = reflect_component { for (i, entity) in archetype.entities().iter().enumerate() { @@ -79,7 +83,10 @@ impl DynamicScene { world: &mut World, entity_map: &mut EntityMap, ) -> Result<(), SceneSpawnError> { - let registry = world.get_resource::().unwrap().clone(); + let registry = world + .get_resource::() + .expect("Could not get `TypeRegistryArc` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?") + .clone(); let type_registry = registry.read(); for scene_entity in &self.entities { @@ -122,7 +129,7 @@ impl DynamicScene { if let Some(map_entities_reflect) = registration.data::() { map_entities_reflect .map_entities(world, entity_map) - .unwrap(); + .unwrap_or_else(|err| panic!("Error mapping entities to world: `{}`", err)); } } @@ -148,5 +155,5 @@ where let mut buf = Vec::new(); let mut ron_serializer = ron::ser::Serializer::new(&mut buf, Some(pretty_config), false)?; serialize.serialize(&mut ron_serializer)?; - Ok(String::from_utf8(buf).unwrap()) + Ok(String::from_utf8(buf).expect("Could not convert serialized ron to UTF-8 string")) } diff --git a/crates/bevy_scene/src/scene_loader.rs b/crates/bevy_scene/src/scene_loader.rs index be8286c95414e..20f0d47e34dd4 100644 --- a/crates/bevy_scene/src/scene_loader.rs +++ b/crates/bevy_scene/src/scene_loader.rs @@ -13,7 +13,9 @@ pub struct SceneLoader { impl FromWorld for SceneLoader { fn from_world(world: &mut World) -> Self { - let type_registry = world.get_resource::().unwrap(); + let type_registry = world + .get_resource::() + .expect("Could not get `TypeRegistryArc` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?"); SceneLoader { type_registry: (*type_registry).clone(), } diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 20962f9fb2130..11357b24f5baa 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -144,7 +144,10 @@ impl SceneSpawner { let mut instance_info = InstanceInfo { entity_map: EntityMap::default(), }; - let type_registry = world.get_resource::().unwrap().clone(); + let type_registry = world + .get_resource::() + .expect("Could not get `TypeRegistryArc` from the `World`. Did you forget to add `ScenePlugin` to your `App`?") + .clone(); let type_registry = type_registry.read(); world.resource_scope(|world, scenes: Mut>| { let scene = @@ -168,7 +171,9 @@ impl SceneSpawner { .expect("component_ids in archetypes should have ComponentInfo"); let reflect_component = type_registry - .get(component_info.type_id().unwrap()) + .get(component_info.type_id().unwrap_or_else(|| { + panic!("Component {:?} has no `type_id`", component_info.name()) + })) .ok_or_else(|| SceneSpawnError::UnregisteredType { type_name: component_info.name().to_string(), }) @@ -192,7 +197,7 @@ impl SceneSpawner { if let Some(map_entities_reflect) = registration.data::() { map_entities_reflect .map_entities(world, &instance_info.entity_map) - .unwrap(); + .unwrap_or_else(|err| panic!("Error mapping entities to world: `{}`", err)); } } self.spawned_instances.insert(instance_id, instance_info); @@ -313,7 +318,7 @@ pub fn scene_spawner_system(world: &mut World) { world.resource_scope(|world, mut scene_spawner: Mut| { let scene_asset_events = world .get_resource::>>() - .unwrap(); + .expect("Could not get `Events>` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?"); let mut updated_spawned_scenes = Vec::new(); let scene_spawner = &mut *scene_spawner; @@ -328,13 +333,15 @@ pub fn scene_spawner_system(world: &mut World) { } } - scene_spawner.despawn_queued_scenes(world).unwrap(); + scene_spawner + .despawn_queued_scenes(world) + .expect("Failed to despawn queued scenes"); scene_spawner .spawn_queued_scenes(world) .unwrap_or_else(|err| panic!("{}", err)); scene_spawner .update_spawned_scenes(world, &updated_spawned_scenes) - .unwrap(); + .expect("Failed to update spawned scenes"); scene_spawner.set_scene_instance_parent_sync(world); }); } diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index a63ca57a96b7d..8629d9366d69f 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -63,7 +63,7 @@ impl Plugin for WindowPlugin { let mut create_window_event = app .world .get_resource_mut::>() - .unwrap(); + .expect("Could not find `Events` resource in the `World`."); create_window_event.send(CreateWindow { id: WindowId::primary(), descriptor: window_descriptor,