Skip to content

Commit 1857ccd

Browse files
committed
Use CommandContext and update docs
1 parent 1e753f8 commit 1857ccd

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

crates/bevy_ecs/src/system/commands/config.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ impl CommandErrorHandler {
6060
///
6161
/// ## Note
6262
/// This is the default behavior if no error handler is specified.
63-
pub fn log<E: Debug>(error: E, _world: &mut World) {
63+
pub fn log<E: Debug>(error: E, _ctx: CommandContext) {
6464
error!("Commands failed with error: {:?}", error)
6565
}
6666

6767
/// If the command failed, [`panic!`] with the error.
68-
pub fn panic<E: Debug>(error: E, _world: &mut World) {
68+
pub fn panic<E: Debug>(error: E, _ctx: CommandContext) {
6969
panic!("Commands failed with error: {:?}", error)
7070
}
7171

7272
/// If the command failed, ignore the error and silently succeed.
73-
pub fn ignore<E: Debug>(_error: E, _world: &mut World) {}
73+
pub fn ignore<E>(_error: E, _ctx: CommandContext) {}
7474
}
7575

7676
pub(crate) struct HandledErrorCommand<C, F>
7777
where
7878
C: FallibleCommand,
79-
F: FnOnce(C::Error, &mut World) + Send + Sync + 'static,
79+
F: FnOnce(C::Error, CommandContext) + Send + Sync + 'static,
8080
{
8181
pub(crate) command: C,
8282
pub(crate) error_handler: F,
@@ -85,7 +85,7 @@ where
8585
impl<C, F> Command for HandledErrorCommand<C, F>
8686
where
8787
C: FallibleCommand,
88-
F: FnOnce(C::Error, &mut World) + Send + Sync + 'static,
88+
F: FnOnce(C::Error, CommandContext) + Send + Sync + 'static,
8989
{
9090
fn write(self: Box<Self>, world: &mut World) {
9191
let HandledErrorCommand {
@@ -94,11 +94,16 @@ where
9494
} = *self;
9595

9696
if let Err(error) = command.try_write(world) {
97-
error_handler(error, world);
97+
error_handler(error, CommandContext { world });
9898
}
9999
}
100100
}
101101

102+
#[non_exhaustive]
103+
pub struct CommandContext<'a> {
104+
pub world: &'a mut World,
105+
}
106+
102107
/// Similar to [`FallibleCommandConfig`] however does not
103108
/// implement [`DerefMut`] nor return `&mut T` of the underlying
104109
/// Commands type.
@@ -147,10 +152,8 @@ macro_rules! impl_fallible_commands {
147152
/// If the command failed, run the provided `error_handler`.
148153
///
149154
/// ## Note
150-
/// This is normally used in conjunction with [`Handlers`].
151-
/// However, this can also be used with custom error handler (e.g. closure)
152-
/// if the parameter type is specific. You can use `on_err_do` if you don't
153-
/// want to specify the type.
155+
/// This is normally used in conjunction with [`CommandErrorHandler`].
156+
/// However, this can also be used with custom error handlers (e.g. closures).
154157
///
155158
/// # Examples
156159
/// ```
@@ -161,17 +164,20 @@ macro_rules! impl_fallible_commands {
161164
/// commands.spawn().insert(42).on_err(CommandErrorHandler::ignore);
162165
///
163166
/// // custom error handler
164-
/// commands.spawn().insert(42).on_err(|error, world| {});
167+
/// commands.spawn().insert(42).on_err(|error, ctx| {});
165168
/// }
166169
/// ```
167170
pub fn on_err(
168171
&mut self,
169-
handler: impl FnOnce(C::Error, &mut World) + Send + Sync + 'static,
172+
error_handler: impl FnOnce(C::Error, CommandContext) + Send + Sync + 'static,
170173
) -> $returnty {
171-
let command = self.command.take().unwrap();
174+
let command = self
175+
.command
176+
.take()
177+
.expect("Cannot call `on_err` multiple times for a command error handler.");
172178
self.inner.add_command(HandledErrorCommand {
173179
command,
174-
error_handler: handler,
180+
error_handler,
175181
});
176182
self.$returnfunc()
177183
}

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl<T> Debug for RemoveBundleError<T> {
531531
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
532532
f.debug_struct("RemoveBundleError")
533533
.field("entity", &self.entity)
534-
.field("component_type", &std::any::type_name::<T>())
534+
.field("bundle_type", &std::any::type_name::<T>())
535535
.finish()
536536
}
537537
}

examples/ecs/command_error_handling.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ fn log_failed_despawn_attempts(attempts: Res<FailedDespawnAttempts>) {
4242

4343
fn despawn_all_entities(mut commands: Commands, query: Query<Entity>) {
4444
for e in query.iter() {
45-
// `on_err_do` allows you to provide a custom error handler!
46-
commands.entity(e).despawn().on_err(|error, world| {
45+
// `on_err` also allows you to provide a custom error handler!
46+
commands.entity(e).despawn().on_err(|error, ctx| {
4747
// You'll notice that the `error` will also give you back the entity
4848
// you tried to despawn.
4949
let entity = error.entity;
5050

5151
warn!("Sadly our entity '{:?}' didn't despawn :(", entity);
5252

5353
// error handlers have mutable access to `World`
54-
if let Some(mut failed_despawns) = world.get_resource_mut::<FailedDespawnAttempts>() {
54+
if let Some(mut failed_despawns) = ctx.world.get_resource_mut::<FailedDespawnAttempts>()
55+
{
5556
failed_despawns.0 += 1;
5657
}
5758
});

0 commit comments

Comments
 (0)