Skip to content

Commit bd4c960

Browse files
authored
Mention Option and When in the error message for a failing system parameter (#19490)
# Objective Help users discover how to use `Option<T>` and `When<T>` to handle failing parameters. ## Solution Have the error message for a failed parameter mention that `Option<T>` and `When<T>` can be used to handle the failure. ## Showcase ``` Encountered an error in system `system_name`: Parameter `Res<ResourceType>` failed validation: Resource does not exist If this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `When<T>` to skip the system when it happens. ```
1 parent 6fee6fe commit bd4c960

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

crates/bevy_ecs/src/system/system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ mod tests {
456456
let result = world.run_system_once(system);
457457

458458
assert!(matches!(result, Err(RunSystemError::InvalidParams { .. })));
459-
let expected = "System bevy_ecs::system::system::tests::run_system_once_invalid_params::system did not run due to failed parameter validation: Parameter `Res<T>` failed validation: Resource does not exist";
459+
let expected = "System bevy_ecs::system::system::tests::run_system_once_invalid_params::system did not run due to failed parameter validation: Parameter `Res<T>` failed validation: Resource does not exist\nIf this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `When<T>` to skip the system when it happens.";
460460
assert_eq!(expected, result.unwrap_err().to_string());
461461
}
462462
}

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ use variadics_please::{all_tuples, all_tuples_enumerated};
151151
/// let mut world = World::new();
152152
/// let err = world.run_system_cached(|param: MyParam| {}).unwrap_err();
153153
/// let expected = "Parameter `MyParam::foo` failed validation: Custom Message";
154-
/// assert!(err.to_string().ends_with(expected));
154+
/// assert!(err.to_string().contains(expected));
155155
/// ```
156156
///
157157
/// ## Builders
@@ -2557,7 +2557,11 @@ impl Display for SystemParamValidationError {
25572557
ShortName(&self.param),
25582558
self.field,
25592559
self.message
2560-
)
2560+
)?;
2561+
if !self.skipped {
2562+
write!(fmt, "\nIf this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `When<T>` to skip the system when it happens.")?;
2563+
}
2564+
Ok(())
25612565
}
25622566
}
25632567

crates/bevy_ecs/src/system/system_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ mod tests {
880880
result,
881881
Err(RegisteredSystemError::InvalidParams { .. })
882882
));
883-
let expected = format!("System {id:?} did not run due to failed parameter validation: Parameter `Res<T>` failed validation: Resource does not exist");
883+
let expected = format!("System {id:?} did not run due to failed parameter validation: Parameter `Res<T>` failed validation: Resource does not exist\nIf this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `When<T>` to skip the system when it happens.");
884884
assert_eq!(expected, result.unwrap_err().to_string());
885885
}
886886

0 commit comments

Comments
 (0)