use alloc::vec::Vec;
use bevy_utils::prelude::DebugName;
use crate::{
component::ComponentId,
entity::{Entity, EntityDoesNotExistError},
schedule::InternedScheduleLabel,
};
#[derive(thiserror::Error, Debug)]
#[error("The schedule with the label {0:?} was not found.")]
pub struct TryRunScheduleError(pub InternedScheduleLabel);
#[derive(thiserror::Error, Debug, Clone)]
#[error("Could not insert bundles of type {bundle_type} into the entities with the following IDs because they do not exist: {entities:?}")]
pub struct TryInsertBatchError {
pub bundle_type: DebugName,
pub entities: Vec<Entity>,
}
#[derive(thiserror::Error, Debug, Clone, Copy)]
#[error("Could not despawn entity: {0}")]
pub struct EntityDespawnError(#[from] pub EntityMutableFetchError);
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntityComponentError {
#[error("The component with ID {0:?} does not exist on the entity.")]
MissingComponent(ComponentId),
#[error("The component with ID {0:?} was requested mutably more than once.")]
AliasedMutability(ComponentId),
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntityMutableFetchError {
#[error(
"{0}\n
If you were attempting to apply a command to this entity,
and want to handle this error gracefully, consider using `EntityCommands::queue_handled` or `queue_silenced`."
)]
EntityDoesNotExist(#[from] EntityDoesNotExistError),
#[error("The entity with ID {0} was requested mutably more than once")]
AliasedMutability(Entity),
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceFetchError {
#[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`?")]
NotRegistered,
#[error("The resource with ID {0:?} does not currently exist in the world.")]
DoesNotExist(ComponentId),
#[error("Cannot get access to the resource with ID {0:?} in the world as it conflicts with an on going operation.")]
NoResourceAccess(ComponentId),
}
#[cfg(test)]
mod tests {
use crate::{
prelude::*,
system::{command::trigger, RunSystemOnce},
};
#[test]
fn fixing_panicking_entity_commands() {
#[derive(EntityEvent)]
struct Kill(Entity);
#[derive(EntityEvent)]
struct FollowupEvent(Entity);
fn despawn(kill: On<Kill>, mut commands: Commands) {
commands.entity(kill.event_target()).despawn();
}
fn followup(kill: On<Kill>, mut commands: Commands) {
commands.queue_silenced(trigger(FollowupEvent(kill.event_target())));
}
let mut world = World::new();
world.add_observer(followup);
world.add_observer(despawn);
world.spawn_empty();
fn kill_everything(mut commands: Commands, query: Query<Entity>) {
for id in query.iter() {
commands.trigger(Kill(id));
}
}
world.run_system_once(kill_everything).unwrap();
}
}