Skip to content

Commit fd8cfa8

Browse files
authored
Add InvalidParameterTypeException (#1027)
* Add InvalidParameterTypeException Used to wrap the ParameterTypeException coming from ParameterValue::get() for improving the error message. Signed-off-by: Jacob Perron <[email protected]> * Describe new exception Signed-off-by: Jacob Perron <[email protected]> * Update tests Signed-off-by: Jacob Perron <[email protected]>
1 parent 01a6bef commit fd8cfa8

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

rclcpp/include/rclcpp/exceptions.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ class InvalidParameterValueException : public std::runtime_error
226226
using std::runtime_error::runtime_error;
227227
};
228228

229+
/// Thrown if requested parameter type is invalid.
230+
/**
231+
* Essentially the same as rclcpp::ParameterTypeException, but with parameter
232+
* name in the error message.
233+
*/
234+
class InvalidParameterTypeException : public std::runtime_error
235+
{
236+
public:
237+
/// Construct an instance.
238+
/**
239+
* \param[in] name the name of the parameter.
240+
* \param[in] message custom exception message.
241+
*/
242+
RCLCPP_PUBLIC
243+
InvalidParameterTypeException(const std::string & name, const std::string message)
244+
: std::runtime_error("parameter '" + name + "' has invalid type: " + message)
245+
{}
246+
};
247+
229248
/// Thrown if parameter is already declared.
230249
class ParameterAlreadyDeclaredException : public std::runtime_error
231250
{

rclcpp/include/rclcpp/node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class Node : public std::enable_shared_from_this<Node>
301301
*
302302
* If the type of the default value, and therefore also the type of return
303303
* value, differs from the initial value provided in the node options, then
304-
* a rclcpp::ParameterTypeException may be thrown.
304+
* a rclcpp::exceptions::InvalidParameterTypeException may be thrown.
305305
* To avoid this, use the declare_parameter() method which returns an
306306
* rclcpp::ParameterValue instead.
307307
*

rclcpp/include/rclcpp/node_impl.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,16 @@ Node::declare_parameter(
156156
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
157157
bool ignore_override)
158158
{
159-
return this->declare_parameter(
160-
name,
161-
rclcpp::ParameterValue(default_value),
162-
parameter_descriptor,
163-
ignore_override
164-
).get<ParameterT>();
159+
try {
160+
return this->declare_parameter(
161+
name,
162+
rclcpp::ParameterValue(default_value),
163+
parameter_descriptor,
164+
ignore_override
165+
).get<ParameterT>();
166+
} catch (const ParameterTypeException & ex) {
167+
throw exceptions::InvalidParameterTypeException(name, ex.what());
168+
}
165169
}
166170

167171
template<typename ParameterT>

rclcpp/include/rclcpp/parameter.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "rcl_interfaces/msg/parameter.hpp"
25+
#include "rclcpp/exceptions.hpp"
2526
#include "rclcpp/parameter_value.hpp"
2627
#include "rclcpp/visibility_control.hpp"
2728

@@ -221,8 +222,12 @@ template<typename T>
221222
decltype(auto)
222223
Parameter::get_value() const
223224
{
224-
// use the helper to specialize for the ParameterValue and Parameter cases.
225-
return detail::get_value_helper<T>(this);
225+
try {
226+
// use the helper to specialize for the ParameterValue and Parameter cases.
227+
return detail::get_value_helper<T>(this);
228+
} catch (const ParameterTypeException & ex) {
229+
throw exceptions::InvalidParameterTypeException(this->name_, ex.what());
230+
}
226231
}
227232

228233
} // namespace rclcpp

rclcpp/test/test_node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
574574
// default type and initial value type do not match
575575
EXPECT_THROW(
576576
{node->declare_parameter("parameter_type_mismatch", 42);},
577-
rclcpp::ParameterTypeException);
577+
rclcpp::exceptions::InvalidParameterTypeException);
578578
}
579579
}
580580

@@ -1717,7 +1717,7 @@ TEST_F(TestNode, get_parameter_undeclared_parameters_not_allowed) {
17171717
int value;
17181718
node->get_parameter(name, value);
17191719
},
1720-
rclcpp::ParameterTypeException);
1720+
rclcpp::exceptions::InvalidParameterTypeException);
17211721
}
17221722
}
17231723

@@ -1951,7 +1951,7 @@ TEST_F(TestNode, get_parameters_undeclared_parameters_not_allowed) {
19511951
{
19521952
node_local->get_parameters("", actual);
19531953
},
1954-
rclcpp::ParameterTypeException);
1954+
rclcpp::exceptions::InvalidParameterTypeException);
19551955
}
19561956
}
19571957
}

0 commit comments

Comments
 (0)