|
1 |
| -//! Contains error and result helpers for use in fallible systems. |
| 1 | +//! Error handling for "fallible" systems. |
| 2 | +//! |
| 3 | +//! When a system is added to a [`Schedule`], and its return type is that of [`Result`], then Bevy |
| 4 | +//! considers those systems to be "fallible", and the ECS scheduler will special-case the [`Err`] |
| 5 | +//! variant of the returned `Result`. |
| 6 | +//! |
| 7 | +//! All [`Error`]s returned by a system are handled by an "error handler". By default, the |
| 8 | +//! [`panic`] error handler function is used, resulting in a panic with the error message attached. |
| 9 | +//! |
| 10 | +//! You can change the default behavior by registering a custom error handler, either globally or |
| 11 | +//! per `Schedule`: |
| 12 | +//! |
| 13 | +//! - [`App::set_system_error_handler`] sets the global error handler for all systems of the |
| 14 | +//! current [`World`]. |
| 15 | +//! - [`Schedule::set_error_handler`] sets the error handler for all systems of that schedule. |
| 16 | +//! |
| 17 | +//! Bevy provides a number of pre-built error-handlers for you to use: |
| 18 | +//! |
| 19 | +//! - [`panic`] – panics with the system error |
| 20 | +//! - [`error`] – logs the system error at the `error` level |
| 21 | +//! - [`warn`] – logs the system error at the `warn` level |
| 22 | +//! - [`info`] – logs the system error at the `info` level |
| 23 | +//! - [`debug`] – logs the system error at the `debug` level |
| 24 | +//! - [`trace`] – logs the system error at the `trace` level |
| 25 | +//! - [`ignore`] – ignores the system error |
| 26 | +//! |
| 27 | +//! However, you can use any custom error handler logic by providing your own function (or |
| 28 | +//! non-capturing closure that coerces to the function signature) as long as it matches the |
| 29 | +//! signature: |
| 30 | +//! |
| 31 | +//! ```rust,ignore |
| 32 | +//! fn(Error, SystemErrorContext) |
| 33 | +//! ``` |
| 34 | +//! |
| 35 | +//! The [`SystemErrorContext`] allows you to access additional details relevant to providing |
| 36 | +//! context surrounding the system error – such as the system's [`name`] – in your error messages. |
| 37 | +//! |
| 38 | +//! For example: |
| 39 | +//! |
| 40 | +//! ```rust |
| 41 | +//! # use bevy_ecs::prelude::*; |
| 42 | +//! # use bevy_ecs::schedule::ScheduleLabel; |
| 43 | +//! # use log::trace; |
| 44 | +//! # fn update() -> Result { Ok(()) } |
| 45 | +//! # #[derive(ScheduleLabel, Hash, Debug, PartialEq, Eq, Clone, Copy)] |
| 46 | +//! # struct MySchedule; |
| 47 | +//! # fn main() { |
| 48 | +//! let mut schedule = Schedule::new(MySchedule); |
| 49 | +//! schedule.add_systems(update); |
| 50 | +//! schedule.set_error_handler(|error, ctx| { |
| 51 | +//! if ctx.name.ends_with("update") { |
| 52 | +//! trace!("Nothing to see here, move along."); |
| 53 | +//! return; |
| 54 | +//! } |
| 55 | +//! |
| 56 | +//! bevy_ecs::result::error(error, ctx); |
| 57 | +//! }); |
| 58 | +//! # } |
| 59 | +//! ``` |
| 60 | +//! |
| 61 | +//! If you need special handling of individual fallible systems, you can use Bevy's [`system piping |
| 62 | +//! feature`] to capture the `Result` output of the system and handle it accordingly. |
| 63 | +//! |
| 64 | +//! [`Schedule`]: crate::schedule::Schedule |
| 65 | +//! [`panic`]: panic() |
| 66 | +//! [`World`]: crate::world::World |
| 67 | +//! [`Schedule::set_error_handler`]: crate::schedule::Schedule::set_error_handler |
| 68 | +//! [`System`]: crate::system::System |
| 69 | +//! [`name`]: crate::system::System::name |
| 70 | +//! [`App::set_system_error_handler`]: ../../bevy_app/struct.App.html#method.set_system_error_handler |
| 71 | +//! [`system piping feature`]: crate::system::In |
2 | 72 |
|
3 |
| -use alloc::boxed::Box; |
| 73 | +use crate::{component::Tick, resource::Resource}; |
| 74 | +use alloc::{borrow::Cow, boxed::Box}; |
4 | 75 |
|
5 | 76 | /// A dynamic error type for use in fallible systems.
|
6 | 77 | pub type Error = Box<dyn core::error::Error + Send + Sync + 'static>;
|
7 | 78 |
|
8 | 79 | /// A result type for use in fallible systems.
|
9 | 80 | pub type Result<T = (), E = Error> = core::result::Result<T, E>;
|
| 81 | + |
| 82 | +/// Additional context for a failed system run. |
| 83 | +pub struct SystemErrorContext { |
| 84 | + /// The name of the system that failed. |
| 85 | + pub name: Cow<'static, str>, |
| 86 | + |
| 87 | + /// The last tick that the system was run. |
| 88 | + pub last_run: Tick, |
| 89 | +} |
| 90 | + |
| 91 | +/// The default systems error handler stored as a resource in the [`World`](crate::world::World). |
| 92 | +pub struct DefaultSystemErrorHandler(pub fn(Error, SystemErrorContext)); |
| 93 | + |
| 94 | +impl Resource for DefaultSystemErrorHandler {} |
| 95 | + |
| 96 | +impl Default for DefaultSystemErrorHandler { |
| 97 | + fn default() -> Self { |
| 98 | + Self(panic) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +macro_rules! inner { |
| 103 | + ($call:path, $e:ident, $c:ident) => { |
| 104 | + $call!("Encountered an error in system `{}`: {:?}", $c.name, $e); |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/// Error handler that panics with the system error. |
| 109 | +#[track_caller] |
| 110 | +#[inline] |
| 111 | +pub fn panic(error: Error, ctx: SystemErrorContext) { |
| 112 | + inner!(panic, error, ctx); |
| 113 | +} |
| 114 | + |
| 115 | +/// Error handler that logs the system error at the `error` level. |
| 116 | +#[track_caller] |
| 117 | +#[inline] |
| 118 | +pub fn error(error: Error, ctx: SystemErrorContext) { |
| 119 | + inner!(log::error, error, ctx); |
| 120 | +} |
| 121 | + |
| 122 | +/// Error handler that logs the system error at the `warn` level. |
| 123 | +#[track_caller] |
| 124 | +#[inline] |
| 125 | +pub fn warn(error: Error, ctx: SystemErrorContext) { |
| 126 | + inner!(log::warn, error, ctx); |
| 127 | +} |
| 128 | + |
| 129 | +/// Error handler that logs the system error at the `info` level. |
| 130 | +#[track_caller] |
| 131 | +#[inline] |
| 132 | +pub fn info(error: Error, ctx: SystemErrorContext) { |
| 133 | + inner!(log::info, error, ctx); |
| 134 | +} |
| 135 | + |
| 136 | +/// Error handler that logs the system error at the `debug` level. |
| 137 | +#[track_caller] |
| 138 | +#[inline] |
| 139 | +pub fn debug(error: Error, ctx: SystemErrorContext) { |
| 140 | + inner!(log::debug, error, ctx); |
| 141 | +} |
| 142 | + |
| 143 | +/// Error handler that logs the system error at the `trace` level. |
| 144 | +#[track_caller] |
| 145 | +#[inline] |
| 146 | +pub fn trace(error: Error, ctx: SystemErrorContext) { |
| 147 | + inner!(log::trace, error, ctx); |
| 148 | +} |
| 149 | + |
| 150 | +/// Error handler that ignores the system error. |
| 151 | +#[track_caller] |
| 152 | +#[inline] |
| 153 | +pub fn ignore(_: Error, _: SystemErrorContext) {} |
0 commit comments