Skip to content

Commit 8bc7023

Browse files
committed
Add panic_fmt method
1 parent 51bfad2 commit 8bc7023

File tree

1 file changed

+17
-12
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+17
-12
lines changed

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,19 @@ impl Command for Despawn {
761761
}
762762
}
763763

764+
/// Utility method for formatting common command panic messages.
765+
fn panic_fmt<Ty>(
766+
verb: &'static str,
767+
trait_name: &'static str,
768+
preposition: &'static str,
769+
entity: Entity,
770+
) -> ! {
771+
panic!("Could not {} a {} (of type `{}`) {} entity {:?} because it doesn't exist in this World.\n\
772+
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
773+
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers",
774+
verb, trait_name, std::any::type_name::<Ty>(), preposition, entity);
775+
}
776+
764777
pub struct InsertBundle<T> {
765778
pub entity: Entity,
766779
pub bundle: T,
@@ -774,9 +787,7 @@ where
774787
if let Some(mut entity) = world.get_entity_mut(self.entity) {
775788
entity.insert_bundle(self.bundle);
776789
} else {
777-
panic!("Could not insert a bundle (of type `{}`) for entity {:?} because it doesn't exist in this World.\n\
778-
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
779-
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers", std::any::type_name::<T>(), self.entity);
790+
panic_fmt::<T>("insert", "bundle", "for", self.entity);
780791
}
781792
}
782793
}
@@ -806,9 +817,7 @@ where
806817
let bundle_opt = if let Some(mut source) = world.get_entity_mut(self.source) {
807818
source.remove_bundle::<T>()
808819
} else {
809-
panic!("Could not move a bundle (of type `{}`) from entity {:?} because it doesn't exist in this World.\n\
810-
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
811-
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers", std::any::type_name::<T>(), self.source);
820+
panic_fmt::<T>("move", "bundle", "from", self.source);
812821
};
813822

814823
let bundle = if let Some(some) = bundle_opt {
@@ -820,9 +829,7 @@ where
820829
if let Some(mut target) = world.get_entity_mut(self.target) {
821830
target.insert_bundle(bundle);
822831
} else {
823-
panic!("Could not move a bundle (of type `{}`) to entity {:?} because it doesn't exist in this World.\n\
824-
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
825-
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers", std::any::type_name::<T>(), self.target);
832+
panic_fmt::<T>("move", "bundle", "to", self.target);
826833
}
827834
}
828835
}
@@ -841,9 +848,7 @@ where
841848
if let Some(mut entity) = world.get_entity_mut(self.entity) {
842849
entity.insert(self.component);
843850
} else {
844-
panic!("Could not add a component (of type `{}`) to entity {:?} because it doesn't exist in this World.\n\
845-
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
846-
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers", std::any::type_name::<T>(), self.entity);
851+
panic_fmt::<T>("add", "component", "to", self.entity);
847852
}
848853
}
849854
}

0 commit comments

Comments
 (0)