Skip to content

Commit 2c39444

Browse files
committed
Use UninitializedStaticallyTypedParameterException
Signed-off-by: Mohammad Farzan <m2_farzan@yahoo.com>
1 parent 942b74c commit 2c39444

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

rclcpp/include/rclcpp/exceptions/exceptions.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,23 @@ class InvalidParameterTypeException : public std::runtime_error
254254
{}
255255
};
256256

257+
/// Thrown if user attempts to create an uninitialized statically typed parameter
258+
/**
259+
* (see https://github.com/ros2/rclcpp/issues/1691)
260+
*/
261+
class UninitializedStaticallyTypedParameterException : public std::runtime_error
262+
{
263+
public:
264+
/// Construct an instance.
265+
/**
266+
* \param[in] name the name of the parameter.
267+
*/
268+
RCLCPP_PUBLIC
269+
explicit UninitializedStaticallyTypedParameterException(const std::string & name)
270+
: std::runtime_error("Statically typed parameter '" + name + "' must be initialized.")
271+
{}
272+
};
273+
257274
/// Thrown if parameter is already declared.
258275
class ParameterAlreadyDeclaredException : public std::runtime_error
259276
{

rclcpp/include/rclcpp/node_impl.hpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,16 @@ Node::declare_parameter(
220220
// get advantage of parameter value template magic to get
221221
// the correct rclcpp::ParameterType from ParameterT
222222
rclcpp::ParameterValue value{ParameterT{}};
223-
return this->declare_parameter(
224-
name,
225-
value.get_type(),
226-
parameter_descriptor,
227-
ignore_override
228-
).get<ParameterT>();
223+
try {
224+
return this->declare_parameter(
225+
name,
226+
value.get_type(),
227+
parameter_descriptor,
228+
ignore_override
229+
).get<ParameterT>();
230+
} catch (const ParameterTypeException & ex) {
231+
throw exceptions::UninitializedStaticallyTypedParameterException(name);
232+
}
229233
}
230234

231235
template<typename ParameterT>

rclcpp/test/rclcpp/test_node.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
690690
"parameter_type_mismatch", rclcpp::ParameterType::PARAMETER_INTEGER);},
691691
rclcpp::exceptions::InvalidParameterTypeException);
692692
}
693+
{
694+
// statically typed parameter must be initialized
695+
EXPECT_THROW(
696+
{node->declare_parameter<std::string>("static_and_uninitialized");},
697+
rclcpp::exceptions::UninitializedStaticallyTypedParameterException);
698+
}
693699
{
694700
// cannot pass an expected type and a descriptor with dynamic_typing=True
695701
rcl_interfaces::msg::ParameterDescriptor descriptor{};

0 commit comments

Comments
 (0)