-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Allow systems to return results #8705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a9484b
implement `IntoSystemConfigs` for results
joseph-gio 232e18e
add a unit test
joseph-gio 632c025
use a zero-cost system adapter with a better error message
joseph-gio 71146db
out-line the panic
joseph-gio cebf349
add an example for system error handling
joseph-gio 30eb55f
Merge remote-tracking branch 'upstream/main' into result-system-config
joseph-gio 20ee20a
fix a merge conflict
joseph-gio 43b30e6
when erroring, suggest using system piping to handle an error
joseph-gio 3fddd1c
Add a doc comment to the example
joseph-gio 8d600bb
Merge branch 'main' into result-system-config
joseph-gio b9f00e5
use system adapters
joseph-gio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //! Demonstrates different strategies that might be used to handle systems that could fail. | ||
|
|
||
| use std::error::Error; | ||
|
|
||
| use bevy_ecs::prelude::*; | ||
|
|
||
| fn main() { | ||
| let mut world = World::new(); | ||
|
|
||
| let mut schedule = Schedule::default(); | ||
| schedule.add_systems(( | ||
| // This system is fallible, which means it returns a Result. | ||
| // If it returns an error, the schedule will panic. | ||
| // To see this happen, try changing this to `fallible_system_2`, | ||
| // which always returns `Err`. | ||
| fallible_system_1, | ||
| // To prevent a fallible system from panicking, we can handle | ||
| // the error by piping it into another system. | ||
| fallible_system_2.pipe(error_handling_system), | ||
| // You can also use `.map()` to handle errors. | ||
| // Bevy includes a number of built-in functions for handling errors, | ||
| // such as `warn` which logs the error using its `Debug` implementation. | ||
| fallible_system_2.map(bevy_utils::warn), | ||
| // If we don't care about a system failing, we can just ignore the error | ||
| // and try again next frame. | ||
| fallible_system_2.map(std::mem::drop), | ||
| )); | ||
|
|
||
| schedule.run(&mut world); | ||
| } | ||
|
|
||
| // A system that might fail. | ||
| // A system can only be added to a schedule if it returns nothing, | ||
| // or if it returns `Result<(), Error>` with an error type that implements std::fmt::Debug. | ||
| // This system always returns `Ok`. | ||
| fn fallible_system_1() -> Result<(), Box<dyn Error>> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Another fallible system. This one always returns `Err`. | ||
| fn fallible_system_2() -> Result<(), Box<dyn Error>> { | ||
| Err("oops")? | ||
| } | ||
|
|
||
| // Our system that we're using to handling errors. | ||
| // Our fallible system returns a Result, so we are taking a Result as an input. | ||
| fn error_handling_system(In(result): In<Result<(), Box<dyn Error>>>) { | ||
| // If the system didn't return an error, we can happily do nothing. | ||
| // If it did return an error, we'll just log it and keep going. | ||
| if let Err(error) = result { | ||
| eprintln!("A system returned an error: {error}"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.