Skip to content

Commit bbb4e5d

Browse files
committed
include examples for command error handling
1 parent 141b5fa commit bbb4e5d

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ path = "examples/ecs/ecs_guide.rs"
255255
name = "change_detection"
256256
path = "examples/ecs/change_detection.rs"
257257

258+
[[example]]
259+
name = "command_error_handling"
260+
path = "examples/ecs/command_error_handling.rs"
261+
258262
[[example]]
259263
name = "event"
260264
path = "examples/ecs/event.rs"

crates/bevy_ecs/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ pub mod prelude {
2727
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
2828
},
2929
system::{
30-
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,
31-
NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
30+
CommandError, Commands, FallibleCommand, In, IntoChainSystem, IntoExclusiveSystem,
31+
IntoSystem, Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res,
32+
ResMut, System,
3233
},
3334
world::{FromWorld, Mut, World},
3435
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
};
1010
use std::{fmt::Debug, marker::PhantomData};
1111

12-
pub use config::{FallibleCommandConfig, FinalFallibleCommandConfig};
13-
pub use fallible::{CommandError, FallibleCommand};
12+
pub use config::*;
13+
pub use fallible::*;
1414

1515
/// A [`World`] mutation.
1616
/// If this could potentially fail, use [`FallibleCommand`].

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Example | File | Description
150150
--- | --- | ---
151151
`ecs_guide` | [`ecs/ecs_guide.rs`](./ecs/ecs_guide.rs) | Full guide to Bevy's ECS
152152
`change_detection` | [`ecs/change_detection.rs`](./ecs/change_detection.rs) | Change detection on components
153+
`command_error_handling` | [`ecs/command_error_handling.rs](./ecs/command_error_handling.rs) | Error handling fallible commands
153154
`event` | [`ecs/event.rs`](./ecs/event.rs) | Illustrates event creation, activation, and reception
154155
`fixed_timestep` | [`ecs/fixed_timestep.rs`](./ecs/fixed_timestep.rs) | Shows how to create systems that run every fixed timestep, rather than every tick
155156
`hierarchy` | [`ecs/hierarchy.rs`](./ecs/hierarchy.rs) | Creates a hierarchy of parents and children entities
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use bevy::prelude::*;
2+
3+
fn main() {
4+
App::build()
5+
.add_startup_system(handle_command_error.system())
6+
.run();
7+
}
8+
9+
#[derive(Debug)]
10+
struct AComponent(usize);
11+
12+
fn handle_command_error(mut commands: Commands) {
13+
let e = commands.spawn().id();
14+
15+
// Immediately despawn the entity.
16+
commands.entity(e).despawn();
17+
18+
// This `despawn` command will fail because the entity was already despawned!
19+
// If no error handler is specified, the error will be logged.
20+
commands.entity(e).despawn();
21+
22+
// Optionally, `on_failure` allows you to provide a custom error handler!
23+
commands
24+
.entity(e)
25+
.insert(AComponent(0))
26+
.on_failure(|CommandError { error, world, .. }| {
27+
// You'll notice that the `error` will also give you back the component you
28+
// attempted to insert on the entity.
29+
30+
println!(
31+
"Sadly our component '{:?}' for entity '{:?}' didn't insert... :(",
32+
error.component, error.entity
33+
);
34+
35+
// error handlers have mutable access to `World`
36+
world.insert_resource("🐦");
37+
});
38+
39+
// Some nice things:
40+
// - You can still chain commands!
41+
// - There are a slew of built-in error handlers
42+
commands
43+
.entity(e)
44+
.insert(AComponent(1))
45+
.ignore() // `ignore` will neither log nor panic the error
46+
.insert(AComponent(2))
47+
.log_on_failure(); // `log_on_failure` is the default behavior, and will log the error
48+
49+
// Uncomment the below line to see the command error cause a panic due to `panic_on_failure`
50+
// commands.entity(e).despawn().panic_on_failure();
51+
}

0 commit comments

Comments
 (0)