//! This module provides panic handlers for [Bevy](https://bevy.org)1//! apps, and automatically configures platform specifics (i.e. Wasm or Android).2//!3//! By default, the [`PanicHandlerPlugin`] from this crate is included in Bevy's `DefaultPlugins`.4//!5//! For more fine-tuned control over panic behavior, disable the [`PanicHandlerPlugin`] or6//! `DefaultPlugins` during app initialization.78use crate::{App, Plugin};910/// Adds sensible panic handlers to Apps. This plugin is part of the `DefaultPlugins`. Adding11/// this plugin will setup a panic hook appropriate to your target platform:12/// * On Wasm, uses [`console_error_panic_hook`](https://crates.io/crates/console_error_panic_hook), logging13/// to the browser console.14/// * Other platforms are currently not setup.15///16/// ```no_run17/// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup, PanicHandlerPlugin};18/// fn main() {19/// App::new()20/// .add_plugins(MinimalPlugins)21/// .add_plugins(PanicHandlerPlugin)22/// .run();23/// }24/// ```25///26/// If you want to setup your own panic handler, you should disable this27/// plugin from `DefaultPlugins`:28/// ```no_run29/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, PanicHandlerPlugin};30/// fn main() {31/// App::new()32/// .add_plugins(DefaultPlugins.build().disable::<PanicHandlerPlugin>())33/// .run();34/// }35/// ```36#[derive(Default)]37pub struct PanicHandlerPlugin;3839impl Plugin for PanicHandlerPlugin {40fn build(&self, _app: &mut App) {41#[cfg(feature = "std")]42{43static SET_HOOK: std::sync::Once = std::sync::Once::new();44SET_HOOK.call_once(|| {45cfg_if::cfg_if! {46if #[cfg(all(target_arch = "wasm32", feature = "web"))] {47// This provides better panic handling in JS engines (displays the panic message and improves the backtrace).48std::panic::set_hook(alloc::boxed::Box::new(console_error_panic_hook::hook));49} else if #[cfg(feature = "error_panic_hook")] {50let current_hook = std::panic::take_hook();51std::panic::set_hook(alloc::boxed::Box::new(52bevy_ecs::error::bevy_error_panic_hook(current_hook),53));54}55// Otherwise use the default target panic hook - Do nothing.56}57});58}59}60}616263