bevy_ecs

Module error

Source
Expand description

Error handling for Bevy systems, commands, and observers.

When a system is added to a Schedule, and its return type is that of Result, then Bevy considers those systems to be “fallible”, and the ECS scheduler will special-case the Err variant of the returned Result.

All BevyErrors returned by a system, observer or command are handled by an “error handler”. By default, the panic error handler function is used, resulting in a panic with the error message attached.

You can change the default behavior by registering a custom error handler. Modify the [GLOBAL_ERROR_HANDLER] value to set a custom error handler function for your entire app. In practice, this is generally feature-flagged: panicking or loudly logging errors in development, and quietly logging or ignoring them in production to avoid crashing the app.

Bevy provides a number of pre-built error-handlers for you to use:

  • panic – panics with the system error
  • error – logs the system error at the error level
  • warn – logs the system error at the warn level
  • info – logs the system error at the info level
  • debug – logs the system error at the debug level
  • trace – logs the system error at the trace level
  • ignore – ignores the system error

However, you can use any custom error handler logic by providing your own function (or non-capturing closure that coerces to the function signature) as long as it matches the signature:

fn(BevyError, ErrorContext)

The ErrorContext allows you to access additional details relevant to providing context surrounding the error – such as the system’s name – in your error messages.

Remember to turn on the configurable_error_handler feature to set a global error handler!

use bevy_ecs::error::{GLOBAL_ERROR_HANDLER, BevyError, ErrorContext};
use log::trace;

fn my_error_handler(error: BevyError, ctx: ErrorContext) {
   if ctx.name().ends_with("plz_ignore") {
      trace!("Nothing to see here, move along.");
      return;
  }
  bevy_ecs::error::error(error, ctx);
}

fn main() {
    // This requires the "configurable_error_handler" feature to be enabled to be in scope.
    GLOBAL_ERROR_HANDLER.set(my_error_handler).expect("The error handler can only be set once.");
     
    // Initialize your Bevy App here
}

If you need special handling of individual fallible systems, you can use Bevy’s system piping feature to capture the Result output of the system and handle it accordingly.

When working with commands, you can handle the result of each command separately using the HandleError::handle_error_with method.

Structs§

  • The built in “universal” Bevy error type. This has a blanket From impl for any type that implements Rust’s Error, meaning it can be used as a “catch all” error.

Enums§

Traits§

Functions§

  • When called, this will skip the currently configured panic hook when a BevyError backtrace has already been printed.
  • Error handler that logs the system error at the debug level.
  • The default error handler. This defaults to panic(), but if set, the [GLOBAL_ERROR_HANDLER] will be used instead, enabling error handler customization. The configurable_error_handler feature must be enabled to change this from the panicking default behavior, as there may be runtime overhead.
  • Error handler that logs the system error at the error level.
  • Error handler that ignores the system error.
  • Error handler that logs the system error at the info level.
  • Error handler that panics with the system error.
  • Error handler that logs the system error at the trace level.
  • Error handler that logs the system error at the warn level.

Type Aliases§

  • A result type for use in fallible systems, commands and observers.