Skip to content

Commit 4fb558a

Browse files
authored
Fix start_type_description_service param handling (#2897)
* Fix `start_type_description_service` param handling Signed-off-by: Patrick Roncagliolo <[email protected]> * Add test Signed-off-by: Patrick Roncagliolo <[email protected]> * Demonstrate different exceptions depending on node options Signed-off-by: Patrick Roncagliolo <[email protected]> * Same exact exception and `what()` message in both cases Signed-off-by: Patrick Roncagliolo <[email protected]> * Uncrustify Signed-off-by: Patrick Roncagliolo <[email protected]> --------- Signed-off-by: Patrick Roncagliolo <[email protected]>
1 parent 2fcef70 commit 4fb558a

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

rclcpp/src/rclcpp/node_interfaces/node_type_descriptions.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,33 @@ class NodeTypeDescriptions::NodeTypeDescriptionsImpl
6565
: logger_(node_logging->get_logger()),
6666
node_base_(node_base)
6767
{
68+
rclcpp::ParameterValue enable_param;
6869
const std::string enable_param_name = "start_type_description_service";
6970

70-
bool enabled = false;
71-
try {
72-
auto enable_param = node_parameters->declare_parameter(
71+
if (!node_parameters->has_parameter(enable_param_name)) {
72+
enable_param = node_parameters->declare_parameter(
7373
enable_param_name,
7474
rclcpp::ParameterValue(true),
7575
rcl_interfaces::msg::ParameterDescriptor()
7676
.set__name(enable_param_name)
7777
.set__type(rclcpp::PARAMETER_BOOL)
7878
.set__description("Start the ~/get_type_description service for this node.")
7979
.set__read_only(true));
80-
enabled = enable_param.get<bool>();
81-
} catch (const rclcpp::exceptions::InvalidParameterTypeException & exc) {
82-
RCLCPP_ERROR(logger_, "%s", exc.what());
83-
throw;
80+
} else {
81+
enable_param = node_parameters->get_parameter(enable_param_name).get_parameter_value();
82+
}
83+
if (enable_param.get_type() != rclcpp::PARAMETER_BOOL) {
84+
RCLCPP_ERROR(
85+
logger_,
86+
"Invalid type '%s' for parameter 'start_type_description_service', should be 'bool'",
87+
rclcpp::to_string(enable_param.get_type()).c_str());
88+
std::ostringstream ss;
89+
ss << "Wrong parameter type, parameter {" << enable_param_name << "} is of type {bool}, "
90+
<< "setting it to {" << to_string(enable_param.get_type()) << "} is not allowed.";
91+
throw rclcpp::exceptions::InvalidParameterTypeException(enable_param_name, ss.str());
8492
}
8593

86-
if (enabled) {
94+
if (enable_param.get<bool>()) {
8795
auto * rcl_node = node_base->get_rcl_node_handle();
8896
std::shared_ptr<rcl_service_t> rcl_srv(
8997
new rcl_service_t,

rclcpp/test/rclcpp/node_interfaces/test_node_type_descriptions.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ TEST_F(TestNodeTypeDescriptions, interface_created)
3737
ASSERT_NE(nullptr, node.get_node_type_descriptions_interface());
3838
}
3939

40+
TEST_F(TestNodeTypeDescriptions, automatically_declare_parameters_from_overrides)
41+
{
42+
rclcpp::NodeOptions node_options;
43+
node_options.automatically_declare_parameters_from_overrides(true);
44+
node_options.append_parameter_override("start_type_description_service", false);
45+
ASSERT_NO_THROW(
46+
{
47+
auto node = std::make_shared<rclcpp::Node>("node", "ns", node_options);
48+
(void) node;
49+
});
50+
}
51+
52+
TEST_F(TestNodeTypeDescriptions, bad_parameter_type)
53+
{
54+
rclcpp::NodeOptions node_options;
55+
node_options.append_parameter_override("start_type_description_service", "unexpected_type");
56+
57+
ASSERT_THROW(
58+
{
59+
node_options.automatically_declare_parameters_from_overrides(false);
60+
auto node = std::make_shared<rclcpp::Node>("node", "ns", node_options);
61+
(void) node;
62+
}, rclcpp::exceptions::InvalidParameterTypeException);
63+
64+
ASSERT_THROW(
65+
{
66+
node_options.automatically_declare_parameters_from_overrides(true);
67+
auto node = std::make_shared<rclcpp::Node>("node", "ns", node_options);
68+
(void) node;
69+
}, rclcpp::exceptions::InvalidParameterTypeException);
70+
}
71+
4072
TEST_F(TestNodeTypeDescriptions, disabled_no_service)
4173
{
4274
rclcpp::NodeOptions node_options;

0 commit comments

Comments
 (0)