diff --git a/crates/bevy_ecs/src/error/bevy_error.rs b/crates/bevy_ecs/src/error/bevy_error.rs index 4c5c2d89b4..0686e68f1d 100644 --- a/crates/bevy_ecs/src/error/bevy_error.rs +++ b/crates/bevy_ecs/src/error/bevy_error.rs @@ -34,48 +34,11 @@ impl BevyError { pub fn downcast_ref(&self) -> Option<&E> { self.inner.error.downcast_ref::() } -} -/// This type exists (rather than having a `BevyError(Box, - #[cfg(feature = "backtrace")] - backtrace: std::backtrace::Backtrace, -} - -// NOTE: writing the impl this way gives us From<&str> ... nice! -impl From for BevyError -where - Box: From, -{ - #[cold] - fn from(error: E) -> Self { - BevyError { - inner: Box::new(InnerBevyError { - error: error.into(), - #[cfg(feature = "backtrace")] - backtrace: std::backtrace::Backtrace::capture(), - }), - } - } -} - -impl Display for BevyError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - writeln!(f, "{}", self.inner.error)?; - Ok(()) - } -} - -impl Debug for BevyError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - writeln!(f, "{:?}", self.inner.error)?; + fn format_backtrace(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { #[cfg(feature = "backtrace")] { + let f = _f; let backtrace = &self.inner.backtrace; if let std::backtrace::BacktraceStatus::Captured = backtrace.status() { let full_backtrace = std::env::var("BEVY_BACKTRACE").is_ok_and(|val| val == "full"); @@ -123,7 +86,50 @@ impl Debug for BevyError { } } } + Ok(()) + } +} +/// This type exists (rather than having a `BevyError(Box, + #[cfg(feature = "backtrace")] + backtrace: std::backtrace::Backtrace, +} + +// NOTE: writing the impl this way gives us From<&str> ... nice! +impl From for BevyError +where + Box: From, +{ + #[cold] + fn from(error: E) -> Self { + BevyError { + inner: Box::new(InnerBevyError { + error: error.into(), + #[cfg(feature = "backtrace")] + backtrace: std::backtrace::Backtrace::capture(), + }), + } + } +} + +impl Display for BevyError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + writeln!(f, "{}", self.inner.error)?; + self.format_backtrace(f)?; + Ok(()) + } +} + +impl Debug for BevyError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + writeln!(f, "{:?}", self.inner.error)?; + self.format_backtrace(f)?; Ok(()) } } diff --git a/crates/bevy_ecs/src/error/handler.rs b/crates/bevy_ecs/src/error/handler.rs index 4fb6507a34..306d1608a1 100644 --- a/crates/bevy_ecs/src/error/handler.rs +++ b/crates/bevy_ecs/src/error/handler.rs @@ -127,7 +127,7 @@ pub fn default_error_handler() -> fn(BevyError, ErrorContext) { macro_rules! inner { ($call:path, $e:ident, $c:ident) => { $call!( - "Encountered an error in {} `{}`: {:?}", + "Encountered an error in {} `{}`: {}", $c.kind(), $c.name(), $e diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index dd87f7da2a..d2cdae38fd 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -2932,7 +2932,7 @@ mod tests { } #[test] - #[should_panic = "Encountered an error in system `bevy_ecs::system::system_param::tests::missing_resource_error::res_system`: SystemParamValidationError { skipped: false, message: \"Resource does not exist\", param: \"bevy_ecs::change_detection::Res\" }"] + #[should_panic = "Encountered an error in system `bevy_ecs::system::system_param::tests::missing_resource_error::res_system`: Parameter `Res` failed validation: Resource does not exist"] fn missing_resource_error() { #[derive(Resource)] pub struct MissingResource; diff --git a/examples/ecs/error_handling.rs b/examples/ecs/error_handling.rs index b4726254d6..b13a018530 100644 --- a/examples/ecs/error_handling.rs +++ b/examples/ecs/error_handling.rs @@ -55,7 +55,7 @@ fn main() { // If we run the app, we'll see the following output at startup: // - // WARN Encountered an error in system `fallible_systems::failing_system`: "Resource not initialized" + // WARN Encountered an error in system `fallible_systems::failing_system`: Resource not initialized // ERROR fallible_systems::failing_system failed: Resource not initialized // INFO captured error: Resource not initialized app.run();