@@ -5,7 +5,11 @@ use crate::{
55    world:: World , 
66} ; 
77use  bevy_utils:: tracing:: debug; 
8- use  std:: marker:: PhantomData ; 
8+ #[ cfg( feature = "command_panic_origin" ) ]  
9+ use  bevy_utils:: tracing:: error; 
10+ #[ cfg( feature = "command_panic_origin" ) ]  
11+ use  std:: panic:: { self ,  AssertUnwindSafe } ; 
12+ use  std:: { borrow:: Cow ,  marker:: PhantomData } ; 
913
1014/// A [`World`] mutation. 
1115pub  trait  Command :  Send  + Sync  + ' static  { 
@@ -15,7 +19,9 @@ pub trait Command: Send + Sync + 'static {
1519/// A queue of [`Command`]s. 
1620#[ derive( Default ) ]  
1721pub  struct  CommandQueue  { 
18-     commands :  Vec < Box < dyn  Command > > , 
22+     pub ( crate )  commands :  Vec < Box < dyn  Command > > , 
23+     #[ allow( unused) ]  
24+     pub ( crate )  system_name :  Option < Cow < ' static ,  str > > , 
1925} 
2026
2127impl  CommandQueue  { 
@@ -24,7 +30,20 @@ impl CommandQueue {
2430pub  fn  apply ( & mut  self ,  world :  & mut  World )  { 
2531        world. flush ( ) ; 
2632        for  command in  self . commands . drain ( ..)  { 
33+             // TODO: replace feature by proper error handling from commands 
34+             #[ cfg( not( feature = "command_panic_origin" ) ) ]  
2735            command. write ( world) ; 
36+             #[ cfg( feature = "command_panic_origin" ) ]  
37+             if  panic:: catch_unwind ( AssertUnwindSafe ( || { 
38+                 command. write ( world) ; 
39+             } ) ) 
40+             . is_err ( ) 
41+             { 
42+                 if  let  Some ( system_name)  = & self . system_name  { 
43+                     error ! ( "panic while applying a command from {}" ,  system_name) ; 
44+                 } 
45+                 panic ! ( "panic applying a command" ) ; 
46+             } 
2847        } 
2948    } 
3049
0 commit comments