File tree Expand file tree Collapse file tree 3 files changed +33
-6
lines changed
Expand file tree Collapse file tree 3 files changed +33
-6
lines changed Original file line number Diff line number Diff 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.
258275class ParameterAlreadyDeclaredException : public std ::runtime_error
259276{
Original file line number Diff line number Diff 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
231235template <typename ParameterT>
Original file line number Diff line number Diff 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{};
You can’t perform that action at this time.
0 commit comments