1use alloc::vec::Vec;
4use bevy_utils::prelude::DebugName;
5
6use crate::{
7 component::ComponentId,
8 entity::{Entity, EntityDoesNotExistError},
9 schedule::InternedScheduleLabel,
10};
11
12#[derive(thiserror::Error, Debug)]
16#[error("The schedule with the label {0:?} was not found.")]
17pub struct TryRunScheduleError(pub InternedScheduleLabel);
18
19#[derive(thiserror::Error, Debug, Clone)]
25#[error("Could not insert bundles of type {bundle_type} into the entities with the following IDs because they do not exist: {entities:?}")]
26pub struct TryInsertBatchError {
27 pub bundle_type: DebugName,
29 pub entities: Vec<Entity>,
31}
32
33#[derive(thiserror::Error, Debug, Clone, Copy)]
35#[error("Could not despawn entity: {0}")]
36pub struct EntityDespawnError(#[from] pub EntityMutableFetchError);
37
38#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
40pub enum EntityComponentError {
41 #[error("The component with ID {0:?} does not exist on the entity.")]
43 MissingComponent(ComponentId),
44 #[error("The component with ID {0:?} was requested mutably more than once.")]
46 AliasedMutability(ComponentId),
47}
48
49#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
51pub enum EntityMutableFetchError {
52 #[error(
54 "{0}\n
55 If you were attempting to apply a command to this entity,
56 and want to handle this error gracefully, consider using `EntityCommands::queue_handled` or `queue_silenced`."
57 )]
58 EntityDoesNotExist(#[from] EntityDoesNotExistError),
59 #[error("The entity with ID {0} was requested mutably more than once")]
61 AliasedMutability(Entity),
62}
63
64#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
66pub enum ResourceFetchError {
67 #[error("The resource has never been initialized or registered with the world. Did you forget to add it using `app.insert_resource` / `app.init_resource`?")]
69 NotRegistered,
70 #[error("The resource with ID {0:?} does not currently exist in the world.")]
72 DoesNotExist(ComponentId),
73 #[error("Cannot get access to the resource with ID {0:?} in the world as it conflicts with an on going operation.")]
75 NoResourceAccess(ComponentId),
76}
77
78#[cfg(test)]
79mod tests {
80 use crate::{
81 prelude::*,
82 system::{command::trigger, RunSystemOnce},
83 };
84
85 #[test]
87 fn fixing_panicking_entity_commands() {
88 #[derive(EntityEvent)]
89 struct Kill(Entity);
90
91 #[derive(EntityEvent)]
92 struct FollowupEvent(Entity);
93
94 fn despawn(kill: On<Kill>, mut commands: Commands) {
95 commands.entity(kill.event_target()).despawn();
96 }
97
98 fn followup(kill: On<Kill>, mut commands: Commands) {
99 commands.queue_silenced(trigger(FollowupEvent(kill.event_target())));
102 }
103
104 let mut world = World::new();
105 world.add_observer(followup);
108 world.add_observer(despawn);
109
110 world.spawn_empty();
112
113 fn kill_everything(mut commands: Commands, query: Query<Entity>) {
115 for id in query.iter() {
116 commands.trigger(Kill(id));
117 }
118 }
119 world.run_system_once(kill_everything).unwrap();
120 }
121}