bevy_ecs/world/
error.rs

1//! Contains error types returned by bevy's schedule.
2
3use alloc::vec::Vec;
4
5use crate::{
6    component::ComponentId,
7    entity::{Entity, EntityDoesNotExistError},
8    schedule::InternedScheduleLabel,
9};
10
11/// The error type returned by [`World::try_run_schedule`] if the provided schedule does not exist.
12///
13/// [`World::try_run_schedule`]: crate::world::World::try_run_schedule
14#[derive(thiserror::Error, Debug)]
15#[error("The schedule with the label {0:?} was not found.")]
16pub struct TryRunScheduleError(pub InternedScheduleLabel);
17
18/// The error type returned by [`World::try_insert_batch`] and [`World::try_insert_batch_if_new`]
19/// if any of the provided entities do not exist.
20///
21/// [`World::try_insert_batch`]: crate::world::World::try_insert_batch
22/// [`World::try_insert_batch_if_new`]: crate::world::World::try_insert_batch_if_new
23#[derive(thiserror::Error, Debug, Clone)]
24#[error("Could not insert bundles of type {bundle_type} into the entities with the following IDs because they do not exist: {entities:?}")]
25pub struct TryInsertBatchError {
26    /// The bundles' type name.
27    pub bundle_type: &'static str,
28    /// The IDs of the provided entities that do not exist.
29    pub entities: Vec<Entity>,
30}
31
32/// An error that occurs when a specified [`Entity`] could not be despawned.
33#[derive(thiserror::Error, Debug, Clone, Copy)]
34#[error("Could not despawn entity: {0}")]
35pub struct EntityDespawnError(#[from] pub EntityMutableFetchError);
36
37/// An error that occurs when dynamically retrieving components from an entity.
38#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
39pub enum EntityComponentError {
40    /// The component with the given [`ComponentId`] does not exist on the entity.
41    #[error("The component with ID {0:?} does not exist on the entity.")]
42    MissingComponent(ComponentId),
43    /// The component with the given [`ComponentId`] was requested mutably more than once.
44    #[error("The component with ID {0:?} was requested mutably more than once.")]
45    AliasedMutability(ComponentId),
46}
47
48/// An error that occurs when fetching entities mutably from a world.
49#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
50pub enum EntityMutableFetchError {
51    /// The entity with the given ID does not exist.
52    #[error(transparent)]
53    EntityDoesNotExist(#[from] EntityDoesNotExistError),
54    /// The entity with the given ID was requested mutably more than once.
55    #[error("The entity with ID {0} was requested mutably more than once")]
56    AliasedMutability(Entity),
57}
58
59/// An error that occurs when getting a resource of a given type in a world.
60#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
61pub enum ResourceFetchError {
62    /// The resource has never been initialized or registered with the world.
63    #[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`?")]
64    NotRegistered,
65    /// The resource with the given [`ComponentId`] does not currently exist in the world.
66    #[error("The resource with ID {0:?} does not currently exist in the world.")]
67    DoesNotExist(ComponentId),
68    /// Cannot get access to the resource with the given [`ComponentId`] in the world as it conflicts with an on going operation.
69    #[error("Cannot get access to the resource with ID {0:?} in the world as it conflicts with an on going operation.")]
70    NoResourceAccess(ComponentId),
71}