Skip to content

Commit 2677862

Browse files
committed
* Fix when to throw the NoParameterOverrideProvided exception.
* Throw an exception when declaring a parameter of a specific type and passing a descriptor with dynamic typing enabled. Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent b9ffd72 commit 2677862

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ declare_parameter_helper(
440440
parameter_descriptor.type = static_cast<uint8_t>(type);
441441
}
442442

443+
if (
444+
rclcpp::PARAMETER_NOT_SET == default_value.get_type() &&
445+
overrides.find(name) == overrides.end() &&
446+
parameter_descriptor.dynamic_typing == false)
447+
{
448+
throw rclcpp::exceptions::NoParameterOverrideProvided(name);
449+
}
450+
443451
rcl_interfaces::msg::ParameterEvent parameter_event;
444452
auto result = __declare_parameter_common(
445453
name,
@@ -461,9 +469,6 @@ declare_parameter_helper(
461469
{
462470
// TODO(ivanpauno): Refactor the logic so we don't need the above `strncmp` and we can
463471
// detect between both exceptions more elegantly.
464-
if (rclcpp::PARAMETER_NOT_SET == default_value.get_type()) {
465-
throw rclcpp::exceptions::NoParameterOverrideProvided(name);
466-
}
467472
throw rclcpp::exceptions::InvalidParameterTypeException(name, result.reason);
468473
}
469474
throw rclcpp::exceptions::InvalidParameterValueException(
@@ -528,6 +533,12 @@ NodeParameters::declare_parameter(
528533
"declare_parameter(): the provided parameter type cannot be rclcpp::PARAMETER_NOT_SET"};
529534
}
530535

536+
if (parameter_descriptor.dynamic_typing == true) {
537+
throw std::invalid_argument{
538+
"declare_parameter(): cannot declare parameter of specific type and pass descriptor"
539+
"with `dynamic_typing=true`"};
540+
}
541+
531542
return declare_parameter_helper(
532543
name,
533544
type,

rclcpp/test/rclcpp/test_node.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,22 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
620620
{node->declare_parameter("parameter_type_mismatch", 42);},
621621
rclcpp::exceptions::InvalidParameterTypeException);
622622
}
623+
{
624+
// default type and expected type do not match
625+
EXPECT_THROW(
626+
{node->declare_parameter(
627+
"parameter_type_mismatch", rclcpp::ParameterType::PARAMETER_INTEGER);},
628+
rclcpp::exceptions::InvalidParameterTypeException);
629+
}
630+
{
631+
// cannot pass an expected type and a descriptor with dynamic_typing=True
632+
rcl_interfaces::msg::ParameterDescriptor descriptor{};
633+
descriptor.dynamic_typing = true;
634+
EXPECT_THROW(
635+
{node->declare_parameter(
636+
"invalid_argument", rclcpp::ParameterType::PARAMETER_INTEGER, descriptor);},
637+
std::invalid_argument);
638+
}
623639
}
624640

625641
TEST_F(TestNode, declare_parameters_with_no_initial_values) {

0 commit comments

Comments
 (0)