2023-10-28 22:20:37 +00:00
use thiserror ::Error ;
2023-09-11 19:04:22 +00:00
use crate ::entity ::Entity ;
/// An error that occurs when retrieving a specific [`Entity`]'s query result from [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState).
// TODO: return the type_name as part of this error
2023-10-28 22:20:37 +00:00
#[ derive(Debug, PartialEq, Eq, Clone, Copy, Error) ]
2023-09-11 19:04:22 +00:00
pub enum QueryEntityError {
/// The given [`Entity`]'s components do not match the query.
///
/// Either it does not have a requested component, or it has a component which the query filters out.
2023-10-28 22:20:37 +00:00
#[ error( " The components of entity {0:?} do not match the query " ) ]
2023-09-11 19:04:22 +00:00
QueryDoesNotMatch ( Entity ) ,
/// The given [`Entity`] does not exist.
2023-10-28 22:20:37 +00:00
#[ error( " The entity {0:?} does not exist " ) ]
2023-09-11 19:04:22 +00:00
NoSuchEntity ( Entity ) ,
/// The [`Entity`] was requested mutably more than once.
///
/// See [`QueryState::get_many_mut`](crate::query::QueryState::get_many_mut) for an example.
2023-10-28 22:20:37 +00:00
#[ error( " The entity {0:?} was requested mutably more than once " ) ]
2023-09-11 19:04:22 +00:00
AliasedMutability ( Entity ) ,
}
/// An error that occurs when retrieving a specific [`Entity`]'s component from a [`Query`](crate::system::Query).
2023-10-28 22:20:37 +00:00
#[ derive(Debug, PartialEq, Eq, Error) ]
2023-09-11 19:04:22 +00:00
pub enum QueryComponentError {
/// The [`Query`](crate::system::Query) does not have read access to the requested component.
///
/// This error occurs when the requested component is not included in the original query.
///
/// # Example
///
/// ```
/// # use bevy_ecs::{prelude::*, query::QueryComponentError};
/// #
/// # #[derive(Component)]
/// # struct OtherComponent;
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # struct RequestedComponent;
/// #
/// # #[derive(Resource)]
/// # struct SpecificEntity {
/// # entity: Entity,
/// # }
/// #
/// fn get_missing_read_access_error(query: Query<&OtherComponent>, res: Res<SpecificEntity>) {
/// assert_eq!(
/// query.get_component::<RequestedComponent>(res.entity),
/// Err(QueryComponentError::MissingReadAccess),
/// );
/// println!("query doesn't have read access to RequestedComponent because it does not appear in Query<&OtherComponent>");
/// }
/// # bevy_ecs::system::assert_is_system(get_missing_read_access_error);
/// ```
2023-10-28 22:20:37 +00:00
#[ error( " This query does not have read access to the requested component " ) ]
2023-09-11 19:04:22 +00:00
MissingReadAccess ,
/// The [`Query`](crate::system::Query) does not have write access to the requested component.
///
/// This error occurs when the requested component is not included in the original query, or the mutability of the requested component is mismatched with the original query.
///
/// # Example
///
/// ```
/// # use bevy_ecs::{prelude::*, query::QueryComponentError};
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # struct RequestedComponent;
/// #
/// # #[derive(Resource)]
/// # struct SpecificEntity {
/// # entity: Entity,
/// # }
/// #
/// fn get_missing_write_access_error(mut query: Query<&RequestedComponent>, res: Res<SpecificEntity>) {
/// assert_eq!(
/// query.get_component::<RequestedComponent>(res.entity),
/// Err(QueryComponentError::MissingWriteAccess),
/// );
/// println!("query doesn't have write access to RequestedComponent because it doesn't have &mut in Query<&RequestedComponent>");
/// }
/// # bevy_ecs::system::assert_is_system(get_missing_write_access_error);
/// ```
2023-10-28 22:20:37 +00:00
#[ error( " This query does not have write access to the requested component " ) ]
2023-09-11 19:04:22 +00:00
MissingWriteAccess ,
/// The given [`Entity`] does not have the requested component.
2023-10-28 22:20:37 +00:00
#[ error( " The given entity does not have the requested component " ) ]
2023-09-11 19:04:22 +00:00
MissingComponent ,
/// The requested [`Entity`] does not exist.
2023-10-28 22:20:37 +00:00
#[ error( " The requested entity does not exist " ) ]
2023-09-11 19:04:22 +00:00
NoSuchEntity ,
}
/// An error that occurs when evaluating a [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState) as a single expected result via
/// [`get_single`](crate::system::Query::get_single) or [`get_single_mut`](crate::system::Query::get_single_mut).
2023-10-28 22:20:37 +00:00
#[ derive(Debug, Error) ]
2023-09-11 19:04:22 +00:00
pub enum QuerySingleError {
/// No entity fits the query.
2023-10-28 22:20:37 +00:00
#[ error( " No entities fit the query {0} " ) ]
2023-09-11 19:04:22 +00:00
NoEntities ( & 'static str ) ,
/// Multiple entities fit the query.
2023-10-28 22:20:37 +00:00
#[ error( " Multiple entities fit the query {0} " ) ]
2023-09-11 19:04:22 +00:00
MultipleEntities ( & 'static str ) ,
}