Bevy system error handling
Decorate your bevy system with the sysfail macro attribute to handle failure.
Before
use *;
use Duration;
use Error;
After
use *;
use *;
use Error;
sysfail attribute
sysfail is an attribute macro you can slap on top of your systems to define
the handling of errors. Unlike pipe, this is done directly at the definition
site, and not when adding to the app. As a result, it's easy to see at a glance
what kind of error handling is happening in the system, it also allows using
the system name as a label in system dependency specification.
The sysfail attribute can only be used on systems returning a type
implementing the Failure trait. Failure is implemented for
Result<(), impl FailureMode> and Option<()>.
sysfail takes a single argument, it is one of the following:
log: print theErrof theResultreturn value, prints a very generic "A none value" when the return type isOption. By default, most things are logged aterrorlevel.log(level = "{silent,trace,debug,info,warn,error}"): This forces logging of errors at a certain level (make sure to add the quotes)ignore: This is a shortcut forlog(level = "silent")
Note that when the level is not "silent", bevy_mod_sysfail adds the
Res<Time> and Local<LoggedErrors> system parameters to be able to supress
repeating error messages.
quick_sysfail attribute
quick_sysfail is like sysfail(ignore) but only works on Option<()>.
This attribute, unlike sysfail allows you to elide the final Some(())
and the type signature of the system. It's for the maximally lazy, like
me.
use *;
// equivalent to:
Traits
How error is handled is not very customizable, but there is a few behaviors controllable by the user, always through traits.
Failure trait
Failure is implemented for Result<(), impl FailureMode> and Option<()>.
Systems marked with the sysfail attribute must return a type implementing
Failure.
FailureMode trait
FailureMode defines how the failure is handled. By implementing the
trait on your own error types, you can specify:
- What constitutes "distinct" error types.
- How long an error must not be produced in order to be displayed again.
FailureMode is implemented for Box<dyn Error>, anyhow::Error, ()
and &'static str.
Change log
See CHANGELOG.md
Version Matrix
| bevy | latest supporting version |
|---|---|
| 0.11 | 4.0.0 |
| 0.10 | 2.0.0 |
| 0.9 | 1.1.0 |
| 0.8 | 0.1.0 |
License
Copyright © 2022 Nicola Papale
This software is licensed under Apache 2.0.