Skip to content

Commit ca81d3e

Browse files
Document query errors (#8692)
# Objective Add documentation to `Query` and `QueryState` errors in bevy_ecs (`QuerySingleError`, `QueryEntityError`, `QueryComponentError`) ## Solution - Change display message for `QueryEntityError::QueryDoesNotMatch`: this error can also happen when the entity has a component which is filtered out (with `Without<C>`) - Fix wrong reference in the documentation of `Query::get_component` and `Query::get_component_mut` from `QueryEntityError` to `QueryComponentError` - Complete the documentation of the three error enum variants. - Add examples for `QueryComponentError::MissingReadAccess` and `QueryComponentError::MissingWriteAccess` - Add reference to `QueryState` in `QueryEntityError`'s documentation. --- ## Migration Guide Expect `QueryEntityError::QueryDoesNotMatch`'s display message to change? Not sure that counts. --------- Co-authored-by: harudagondi <giogdeasis@gmail.com>
1 parent 292e069 commit ca81d3e

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

crates/bevy_ecs/src/query/state.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,12 +1162,19 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
11621162
}
11631163
}
11641164

1165-
/// An error that occurs when retrieving a specific [`Entity`]'s query result.
1165+
/// An error that occurs when retrieving a specific [`Entity`]'s query result from [`Query`](crate::system::Query) or [`QueryState`].
11661166
// TODO: return the type_name as part of this error
11671167
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
11681168
pub enum QueryEntityError {
1169+
/// The given [`Entity`]'s components do not match the query.
1170+
///
1171+
/// Either it does not have a requested component, or it has a component which the query filters out.
11691172
QueryDoesNotMatch(Entity),
1173+
/// The given [`Entity`] does not exist.
11701174
NoSuchEntity(Entity),
1175+
/// The [`Entity`] was requested mutably more than once.
1176+
///
1177+
/// See [`QueryState::get_many_mut`] for an example.
11711178
AliasedMutability(Entity),
11721179
}
11731180

@@ -1177,7 +1184,7 @@ impl fmt::Display for QueryEntityError {
11771184
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11781185
match self {
11791186
QueryEntityError::QueryDoesNotMatch(_) => {
1180-
write!(f, "The given entity does not have the requested component.")
1187+
write!(f, "The given entity's components do not match the query.")
11811188
}
11821189
QueryEntityError::NoSuchEntity(_) => write!(f, "The requested entity does not exist."),
11831190
QueryEntityError::AliasedMutability(_) => {
@@ -1296,11 +1303,13 @@ mod tests {
12961303
}
12971304
}
12981305

1299-
/// An error that occurs when evaluating a [`QueryState`] as a single expected resulted via
1300-
/// [`QueryState::single`] or [`QueryState::single_mut`].
1306+
/// An error that occurs when evaluating a [`Query`](crate::system::Query) or [`QueryState`] as a single expected result via
1307+
/// [`get_single`](crate::system::Query::get_single) or [`get_single_mut`](crate::system::Query::get_single_mut).
13011308
#[derive(Debug)]
13021309
pub enum QuerySingleError {
1310+
/// No entity fits the query.
13031311
NoEntities(&'static str),
1312+
/// Multiple entities fit the query.
13041313
MultipleEntities(&'static str),
13051314
}
13061315

crates/bevy_ecs/src/system/query.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
10541054

10551055
/// Returns a mutable reference to the component `T` of the given entity.
10561056
///
1057-
/// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is returned instead.
1057+
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.
10581058
///
10591059
/// # Example
10601060
///
@@ -1090,7 +1090,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
10901090

10911091
/// Returns a mutable reference to the component `T` of the given entity.
10921092
///
1093-
/// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is returned instead.
1093+
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.
10941094
///
10951095
/// # Safety
10961096
///
@@ -1357,12 +1357,69 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> IntoIterator for &'w mut Quer
13571357
}
13581358
}
13591359

1360-
/// An error that occurs when retrieving a specific [`Entity`]'s component from a [`Query`]
1360+
/// An error that occurs when retrieving a specific [`Entity`]'s component from a [`Query`].
13611361
#[derive(Debug, PartialEq, Eq)]
13621362
pub enum QueryComponentError {
1363+
/// The [`Query`] does not have read access to the requested component.
1364+
///
1365+
/// This error occurs when the requested component is not included in the original query.
1366+
///
1367+
/// # Example
1368+
///
1369+
/// ```
1370+
/// # use bevy_ecs::{prelude::*, system::QueryComponentError};
1371+
/// #
1372+
/// # #[derive(Component)]
1373+
/// # struct OtherComponent;
1374+
/// #
1375+
/// # #[derive(Component, PartialEq, Debug)]
1376+
/// # struct RequestedComponent;
1377+
/// #
1378+
/// # #[derive(Resource)]
1379+
/// # struct SpecificEntity {
1380+
/// # entity: Entity,
1381+
/// # }
1382+
/// #
1383+
/// fn get_missing_read_access_error(query: Query<&OtherComponent>, res: Res<SpecificEntity>) {
1384+
/// assert_eq!(
1385+
/// query.get_component::<RequestedComponent>(res.entity),
1386+
/// Err(QueryComponentError::MissingReadAccess),
1387+
/// );
1388+
/// println!("query doesn't have read access to RequestedComponent because it does not appear in Query<&OtherComponent>");
1389+
/// }
1390+
/// # bevy_ecs::system::assert_is_system(get_missing_read_access_error);
1391+
/// ```
13631392
MissingReadAccess,
1393+
/// The [`Query`] does not have write access to the requested component.
1394+
///
1395+
/// 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.
1396+
///
1397+
/// # Example
1398+
///
1399+
/// ```
1400+
/// # use bevy_ecs::{prelude::*, system::QueryComponentError};
1401+
/// #
1402+
/// # #[derive(Component, PartialEq, Debug)]
1403+
/// # struct RequestedComponent;
1404+
/// #
1405+
/// # #[derive(Resource)]
1406+
/// # struct SpecificEntity {
1407+
/// # entity: Entity,
1408+
/// # }
1409+
/// #
1410+
/// fn get_missing_write_access_error(mut query: Query<&RequestedComponent>, res: Res<SpecificEntity>) {
1411+
/// assert_eq!(
1412+
/// query.get_component::<RequestedComponent>(res.entity),
1413+
/// Err(QueryComponentError::MissingWriteAccess),
1414+
/// );
1415+
/// println!("query doesn't have write access to RequestedComponent because it doesn't have &mut in Query<&RequestedComponent>");
1416+
/// }
1417+
/// # bevy_ecs::system::assert_is_system(get_missing_write_access_error);
1418+
/// ```
13641419
MissingWriteAccess,
1420+
/// The given [`Entity`] does not have the requested component.
13651421
MissingComponent,
1422+
/// The requested [`Entity`] does not exist.
13661423
NoSuchEntity,
13671424
}
13681425

0 commit comments

Comments
 (0)