forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate out
PanicHandlerPlugin
(bevyengine#12557)
# Objective - Allow configuring of platform-specific panic handlers. - Remove the silent overwrite of the WASM panic handler - Closes bevyengine#12546 ## Solution - Separates the panic handler to a new plugin, `PanicHandlerPlugin`. - `PanicHandlerPlugin` was added to `DefaultPlugins`. - Can be disabled on `DefaultPlugins`, in the case someone needs to configure custom panic handlers. --- ## Changelog ### Added - A `PanicHandlerPlugin` was added to the `DefaultPlugins`, which now sets sensible target-specific panic handlers. ### Changed - On WASM, the panic stack trace was output to the console through the `BevyLogPlugin`. Since this was separated out into `PanicHandlerPlugin`, you may need to add the new `PanicHandlerPlugin` (included in `DefaultPlugins`). ## Migration Guide - If you used `MinimalPlugins` with `LogPlugin` for a WASM-target build, you will need to add the new `PanicHandlerPlugin` to set the panic behavior to output to the console. Otherwise, you will see the default panic handler (opaque, `unreachable` errors in the console).
- Loading branch information
Showing
7 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,23 @@ | ||
[package] | ||
name = "bevy_panic_handler" | ||
version = "0.14.0-dev" | ||
edition = "2021" | ||
description = "Provides panic handlers for Bevy Engine" | ||
homepage = "https://bevyengine.org" | ||
repository = "https://github.com/bevyengine/bevy" | ||
license = "MIT OR Apache-2.0" | ||
keywords = ["bevy"] | ||
|
||
[features] | ||
|
||
[dependencies] | ||
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" } | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dependencies] | ||
console_error_panic_hook = "0.1.6" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true |
This file contains 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,54 @@ | ||
//! This crate provides panic handlers for [Bevy](https://bevyengine.org) | ||
//! apps, and automatically configures platform specifics (i.e. WASM or Android). | ||
//! | ||
//! By default, the [`PanicHandlerPlugin`] from this crate is included in Bevy's `DefaultPlugins`. | ||
//! | ||
//! For more fine-tuned control over panic behavior, disable the [`PanicHandlerPlugin`] or | ||
//! `DefaultPlugins` during app initialization. | ||
#![cfg_attr(docsrs, feature(doc_auto_cfg))] | ||
|
||
use bevy_app::{App, Plugin}; | ||
|
||
/// Adds sensible panic handlers to Apps. This plugin is part of the `DefaultPlugins`. Adding | ||
/// this plugin will setup a panic hook appropriate to your target platform: | ||
/// * On WASM, uses [`console_error_panic_hook`](https://crates.io/crates/console_error_panic_hook), logging | ||
/// to the browser console. | ||
/// * Other platforms are currently not setup. | ||
/// | ||
/// ```no_run | ||
/// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup}; | ||
/// # use bevy_panic_handler::PanicHandlerPlugin; | ||
/// fn main() { | ||
/// App::new() | ||
/// .add_plugins(MinimalPlugins) | ||
/// .add_plugins(PanicHandlerPlugin) | ||
/// .run(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// If you want to setup your own panic handler, you should disable this | ||
/// plugin from `DefaultPlugins`: | ||
/// ```no_run | ||
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup}; | ||
/// # use bevy_panic_handler::PanicHandlerPlugin; | ||
/// fn main() { | ||
/// App::new() | ||
/// .add_plugins(DefaultPlugins.build().disable::<PanicHandlerPlugin>()) | ||
/// .run(); | ||
/// } | ||
/// ``` | ||
#[derive(Default)] | ||
pub struct PanicHandlerPlugin; | ||
|
||
impl Plugin for PanicHandlerPlugin { | ||
fn build(&self, _app: &mut App) { | ||
#[cfg(target_arch = "wasm32")] | ||
{ | ||
console_error_panic_hook::set_once(); | ||
} | ||
#[cfg(not(target_arch = "wasm32"))] | ||
{ | ||
// Use the default target panic hook - Do nothing. | ||
} | ||
} | ||
} |