@@ -60,23 +60,23 @@ impl CommandErrorHandler {
60
60
///
61
61
/// ## Note
62
62
/// 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 ) {
64
64
error ! ( "Commands failed with error: {:?}" , error)
65
65
}
66
66
67
67
/// 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 ) {
69
69
panic ! ( "Commands failed with error: {:?}" , error)
70
70
}
71
71
72
72
/// 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 ) { }
74
74
}
75
75
76
76
pub ( crate ) struct HandledErrorCommand < C , F >
77
77
where
78
78
C : FallibleCommand ,
79
- F : FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
79
+ F : FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
80
80
{
81
81
pub ( crate ) command : C ,
82
82
pub ( crate ) error_handler : F ,
85
85
impl < C , F > Command for HandledErrorCommand < C , F >
86
86
where
87
87
C : FallibleCommand ,
88
- F : FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
88
+ F : FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
89
89
{
90
90
fn write ( self : Box < Self > , world : & mut World ) {
91
91
let HandledErrorCommand {
@@ -94,11 +94,16 @@ where
94
94
} = * self ;
95
95
96
96
if let Err ( error) = command. try_write ( world) {
97
- error_handler ( error, world) ;
97
+ error_handler ( error, CommandContext { world } ) ;
98
98
}
99
99
}
100
100
}
101
101
102
+ #[ non_exhaustive]
103
+ pub struct CommandContext < ' a > {
104
+ pub world : & ' a mut World ,
105
+ }
106
+
102
107
/// Similar to [`FallibleCommandConfig`] however does not
103
108
/// implement [`DerefMut`] nor return `&mut T` of the underlying
104
109
/// Commands type.
@@ -147,10 +152,8 @@ macro_rules! impl_fallible_commands {
147
152
/// If the command failed, run the provided `error_handler`.
148
153
///
149
154
/// ## 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).
154
157
///
155
158
/// # Examples
156
159
/// ```
@@ -161,17 +164,20 @@ macro_rules! impl_fallible_commands {
161
164
/// commands.spawn().insert(42).on_err(CommandErrorHandler::ignore);
162
165
///
163
166
/// // custom error handler
164
- /// commands.spawn().insert(42).on_err(|error, world | {});
167
+ /// commands.spawn().insert(42).on_err(|error, ctx | {});
165
168
/// }
166
169
/// ```
167
170
pub fn on_err(
168
171
& mut self ,
169
- handler : impl FnOnce ( C :: Error , & mut World ) + Send + Sync + ' static ,
172
+ error_handler : impl FnOnce ( C :: Error , CommandContext ) + Send + Sync + ' static ,
170
173
) -> $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." ) ;
172
178
self . inner. add_command( HandledErrorCommand {
173
179
command,
174
- error_handler: handler ,
180
+ error_handler,
175
181
} ) ;
176
182
self . $returnfunc( )
177
183
}
0 commit comments