Crate bevy_mod_try_system
source ·Expand description
bevy_mod_try_system
provides an extension trait for bevy systems that output Result
types
which can distinctly pipe Err
and Ok
values to separate output systems.
This is achieved via the system.pipe_err(err_system)
method, which passes Err
variants as
input to the err_system
system, and passes Ok
variants to subsequent pipe
calls.
In particular, this is useful for managing application-level error context. Since the most
common (at least to start) error handling technique might be “log the error”, a utility log_err
method, which calls bevy::log::error!
on any Err
results, is also provided.
§Example
In this example, we add an error to a bucket every other update.
use bevy::prelude::*;
use bevy_mod_try_system::*;
#[derive(Debug)]
struct TestError;
#[derive(Default, Resource)]
struct Bucket(Vec<TestError>);
// N.B. We could use `?` in this system, which is convenient.
fn increment_and_error_if_even(mut counter: Local<usize>) -> Result<(), TestError> {
*counter += 1;
if *counter % 2 == 1 {
Ok(())
} else {
Err(TestError)
}
}
fn handle_error(In(error): In<TestError>, mut bucket: ResMut<Bucket>) {
bucket.0.push(error);
}
fn main() {
let mut app = App::new();
app.init_resource::<Bucket>();
app.add_systems(Update, increment_and_error_if_even.pipe_err(handle_error));
assert_eq!(app.world().resource::<Bucket>().0.len(), 0);
app.update();
assert_eq!(app.world().resource::<Bucket>().0.len(), 0);
app.update();
assert_eq!(app.world().resource::<Bucket>().0.len(), 1);
app.update();
assert_eq!(app.world().resource::<Bucket>().0.len(), 1);
}
Consider using or referencing bevy_anyhow_alerts
to see how to extend this to manage
system- and application-level errors.
Traits§
- Any system that outputs a Result is a TrySystem.
Type Aliases§
- An AdapterSystem for systems that return Results and want to log errors.
- A CombinatorSystem for systems that return Results and handle errors.