panic on system error (#16979)

# Objective

- First step for #16718 
- #16589 introduced an api that can only ignore errors, which is risky

## Solution

- Panic instead of just ignoring the errors

## Testing

- Changed the `fallible_systems` example to return an error
```
Encountered an error in system `fallible_systems::setup`: TooManyVertices { subdivisions: 300, number_of_resulting_points: 906012 }
Encountered a panic in system `fallible_systems::setup`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
```
This commit is contained in:
François Mockers 2024-12-27 00:44:46 +01:00 committed by GitHub
parent c03e494a26
commit 394e82f4bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 11 deletions

View File

@ -605,11 +605,17 @@ impl ExecutorState {
// access the world data used by the system. // access the world data used by the system.
// - `update_archetype_component_access` has been called. // - `update_archetype_component_access` has been called.
unsafe { unsafe {
// TODO: implement an error-handling API instead of suppressing a possible failure. // TODO: implement an error-handling API instead of panicking.
let _ = __rust_begin_short_backtrace::run_unsafe( if let Err(err) = __rust_begin_short_backtrace::run_unsafe(
system, system,
context.environment.world_cell, context.environment.world_cell,
); ) {
panic!(
"Encountered an error in system `{}`: {:?}",
&*system.name(),
err
);
};
}; };
})); }));
context.system_completed(system_index, res, system); context.system_completed(system_index, res, system);
@ -653,8 +659,14 @@ impl ExecutorState {
// that no other systems currently have access to the world. // that no other systems currently have access to the world.
let world = unsafe { context.environment.world_cell.world_mut() }; let world = unsafe { context.environment.world_cell.world_mut() };
let res = std::panic::catch_unwind(AssertUnwindSafe(|| { let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
// TODO: implement an error-handling API instead of suppressing a possible failure. // TODO: implement an error-handling API instead of panicking.
let _ = __rust_begin_short_backtrace::run(system, world); if let Err(err) = __rust_begin_short_backtrace::run(system, world) {
panic!(
"Encountered an error in system `{}`: {:?}",
&*system.name(),
err
);
};
})); }));
context.system_completed(system_index, res, system); context.system_completed(system_index, res, system);
}; };

View File

@ -101,8 +101,14 @@ impl SystemExecutor for SimpleExecutor {
} }
let f = AssertUnwindSafe(|| { let f = AssertUnwindSafe(|| {
// TODO: implement an error-handling API instead of suppressing a possible failure. // TODO: implement an error-handling API instead of panicking.
let _ = __rust_begin_short_backtrace::run(system, world); if let Err(err) = __rust_begin_short_backtrace::run(system, world) {
panic!(
"Encountered an error in system `{}`: {:?}",
&*system.name(),
err
);
}
}); });
#[cfg(feature = "std")] #[cfg(feature = "std")]

View File

@ -109,8 +109,14 @@ impl SystemExecutor for SingleThreadedExecutor {
let f = AssertUnwindSafe(|| { let f = AssertUnwindSafe(|| {
if system.is_exclusive() { if system.is_exclusive() {
// TODO: implement an error-handling API instead of suppressing a possible failure. // TODO: implement an error-handling API instead of panicking.
let _ = __rust_begin_short_backtrace::run(system, world); if let Err(err) = __rust_begin_short_backtrace::run(system, world) {
panic!(
"Encountered an error in system `{}`: {:?}",
&*system.name(),
err
);
}
} else { } else {
// Use run_unsafe to avoid immediately applying deferred buffers // Use run_unsafe to avoid immediately applying deferred buffers
let world = world.as_unsafe_world_cell(); let world = world.as_unsafe_world_cell();
@ -118,8 +124,14 @@ impl SystemExecutor for SingleThreadedExecutor {
// SAFETY: We have exclusive, single-threaded access to the world and // SAFETY: We have exclusive, single-threaded access to the world and
// update_archetype_component_access is being called immediately before this. // update_archetype_component_access is being called immediately before this.
unsafe { unsafe {
// TODO: implement an error-handling API instead of suppressing a possible failure. // TODO: implement an error-handling API instead of panicking.
let _ = __rust_begin_short_backtrace::run_unsafe(system, world); if let Err(err) = __rust_begin_short_backtrace::run_unsafe(system, world) {
panic!(
"Encountered an error in system `{}`: {:?}",
&*system.name(),
err
);
}
}; };
} }
}); });

View File

@ -1766,6 +1766,7 @@ mod tests {
} }
#[test] #[test]
#[should_panic]
fn simple_fallible_system() { fn simple_fallible_system() {
fn sys() -> Result { fn sys() -> Result {
Err("error")?; Err("error")?;