@@ -60,23 +60,23 @@ impl CommandErrorHandler {
6060 ///
6161 /// ## Note
6262 /// This is the default behavior if no error handler is specified.
63- pub fn log < E : Debug > ( error : E , _world : & mut World ) {
63+ pub fn log < E : Debug > ( error : E , _ctx : CommandContext ) {
6464 error ! ( "Commands failed with error: {:?}" , error)
6565 }
6666
6767 /// If the command failed, [`panic!`] with the error.
68- pub fn panic < E : Debug > ( error : E , _world : & mut World ) {
68+ pub fn panic < E : Debug > ( error : E , _ctx : CommandContext ) {
6969 panic ! ( "Commands failed with error: {:?}" , error)
7070 }
7171
7272 /// If the command failed, ignore the error and silently succeed.
73- pub fn ignore < E : Debug > ( _error : E , _world : & mut World ) { }
73+ pub fn ignore < E > ( _error : E , _ctx : CommandContext ) { }
7474}
7575
7676pub ( crate ) struct HandledErrorCommand < C , F >
7777where
7878 C : FallibleCommand ,
79- F : FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
79+ F : FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
8080{
8181 pub ( crate ) command : C ,
8282 pub ( crate ) error_handler : F ,
8585impl < C , F > Command for HandledErrorCommand < C , F >
8686where
8787 C : FallibleCommand ,
88- F : FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
88+ F : FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
8989{
9090 fn write ( self : Box < Self > , world : & mut World ) {
9191 let HandledErrorCommand {
@@ -94,11 +94,16 @@ where
9494 } = * self ;
9595
9696 if let Err ( error) = command. try_write ( world) {
97- error_handler ( error, world) ;
97+ error_handler ( error, CommandContext { world } ) ;
9898 }
9999 }
100100}
101101
102+ #[ non_exhaustive]
103+ pub struct CommandContext < ' a > {
104+ pub world : & ' a mut World ,
105+ }
106+
102107/// Similar to [`FallibleCommandConfig`] however does not
103108/// implement [`DerefMut`] nor return `&mut T` of the underlying
104109/// Commands type.
@@ -147,10 +152,8 @@ macro_rules! impl_fallible_commands {
147152 /// If the command failed, run the provided `error_handler`.
148153 ///
149154 /// ## Note
150- /// This is normally used in conjunction with [`Handlers`].
151- /// However, this can also be used with custom error handler (e.g. closure)
152- /// if the parameter type is specific. You can use `on_err_do` if you don't
153- /// want to specify the type.
155+ /// This is normally used in conjunction with [`CommandErrorHandler`].
156+ /// However, this can also be used with custom error handlers (e.g. closures).
154157 ///
155158 /// # Examples
156159 /// ```
@@ -161,17 +164,20 @@ macro_rules! impl_fallible_commands {
161164 /// commands.spawn().insert(42).on_err(CommandErrorHandler::ignore);
162165 ///
163166 /// // custom error handler
164- /// commands.spawn().insert(42).on_err(|error, world | {});
167+ /// commands.spawn().insert(42).on_err(|error, ctx | {});
165168 /// }
166169 /// ```
167170 pub fn on_err(
168171 & mut self ,
169- handler : impl FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
172+ error_handler : impl FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
170173 ) -> $returnty {
171- let command = self . command. take( ) . unwrap( ) ;
174+ let command = self
175+ . command
176+ . take( )
177+ . expect( "Cannot call `on_err` multiple times for a command error handler." ) ;
172178 self . inner. add_command( HandledErrorCommand {
173179 command,
174- error_handler: handler ,
180+ error_handler,
175181 } ) ;
176182 self . $returnfunc( )
177183 }
0 commit comments