Skip to content

Commit 7a2ee23

Browse files
m2-farzanivanpauno
andauthored
Use UninitializedStaticallyTypedParameterException (#1689)
* Use UninitializedStaticallyTypedParameterException Signed-off-by: Mohammad Farzan <[email protected]> Co-authored-by: Ivan Santiago Paunovic <[email protected]>
1 parent 802bfc2 commit 7a2ee23

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

rclcpp/include/rclcpp/exceptions/exceptions.hpp

Lines changed: 17 additions & 0 deletions
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

Lines changed: 10 additions & 6 deletions
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 &) {
231+
throw exceptions::UninitializedStaticallyTypedParameterException(name);
232+
}
229233
}
230234

231235
template<typename ParameterT>

rclcpp/test/rclcpp/test_node.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,12 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
715715
"parameter_type_mismatch", rclcpp::ParameterType::PARAMETER_INTEGER);},
716716
rclcpp::exceptions::InvalidParameterTypeException);
717717
}
718+
{
719+
// statically typed parameter must be initialized
720+
EXPECT_THROW(
721+
{node->declare_parameter<std::string>("static_and_uninitialized");},
722+
rclcpp::exceptions::UninitializedStaticallyTypedParameterException);
723+
}
718724
{
719725
// cannot pass an expected type and a descriptor with dynamic_typing=True
720726
rcl_interfaces::msg::ParameterDescriptor descriptor{};

0 commit comments

Comments
 (0)