bevy_app/
panic_handler.rs

1//! This module provides panic handlers for [Bevy](https://bevyengine.org)
2//! apps, and automatically configures platform specifics (i.e. WASM or Android).
3//!
4//! By default, the [`PanicHandlerPlugin`] from this crate is included in Bevy's `DefaultPlugins`.
5//!
6//! For more fine-tuned control over panic behavior, disable the [`PanicHandlerPlugin`] or
7//! `DefaultPlugins` during app initialization.
8
9use crate::App;
10use crate::Plugin;
11
12/// Adds sensible panic handlers to Apps. This plugin is part of the `DefaultPlugins`. Adding
13/// this plugin will setup a panic hook appropriate to your target platform:
14/// * On WASM, uses [`console_error_panic_hook`](https://crates.io/crates/console_error_panic_hook), logging
15/// to the browser console.
16/// * Other platforms are currently not setup.
17///
18/// ```no_run
19/// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup, PanicHandlerPlugin};
20/// fn main() {
21///     App::new()
22///         .add_plugins(MinimalPlugins)
23///         .add_plugins(PanicHandlerPlugin)
24///         .run();
25/// }
26/// ```
27///
28/// If you want to setup your own panic handler, you should disable this
29/// plugin from `DefaultPlugins`:
30/// ```no_run
31/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, PanicHandlerPlugin};
32/// fn main() {
33///     App::new()
34///         .add_plugins(DefaultPlugins.build().disable::<PanicHandlerPlugin>())
35///         .run();
36/// }
37/// ```
38#[derive(Default)]
39pub struct PanicHandlerPlugin;
40
41impl Plugin for PanicHandlerPlugin {
42    fn build(&self, _app: &mut App) {
43        #[cfg(target_arch = "wasm32")]
44        {
45            console_error_panic_hook::set_once();
46        }
47        #[cfg(not(target_arch = "wasm32"))]
48        {
49            // Use the default target panic hook - Do nothing.
50        }
51    }
52}