Add InvalidParameterTypeException (#1027)
* Add InvalidParameterTypeException Used to wrap the ParameterTypeException coming from ParameterValue::get() for improving the error message. Signed-off-by: Jacob Perron <jacob@openrobotics.org> * Describe new exception Signed-off-by: Jacob Perron <jacob@openrobotics.org> * Update tests Signed-off-by: Jacob Perron <jacob@openrobotics.org>
This commit is contained in:
parent
01a6befdde
commit
fd8cfa8fe3
5 changed files with 40 additions and 12 deletions
|
@ -226,6 +226,25 @@ class InvalidParameterValueException : public std::runtime_error
|
|||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
/// Thrown if requested parameter type is invalid.
|
||||
/**
|
||||
* Essentially the same as rclcpp::ParameterTypeException, but with parameter
|
||||
* name in the error message.
|
||||
*/
|
||||
class InvalidParameterTypeException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
/// Construct an instance.
|
||||
/**
|
||||
* \param[in] name the name of the parameter.
|
||||
* \param[in] message custom exception message.
|
||||
*/
|
||||
RCLCPP_PUBLIC
|
||||
InvalidParameterTypeException(const std::string & name, const std::string message)
|
||||
: std::runtime_error("parameter '" + name + "' has invalid type: " + message)
|
||||
{}
|
||||
};
|
||||
|
||||
/// Thrown if parameter is already declared.
|
||||
class ParameterAlreadyDeclaredException : public std::runtime_error
|
||||
{
|
||||
|
|
|
@ -301,7 +301,7 @@ public:
|
|||
*
|
||||
* If the type of the default value, and therefore also the type of return
|
||||
* value, differs from the initial value provided in the node options, then
|
||||
* a rclcpp::ParameterTypeException may be thrown.
|
||||
* a rclcpp::exceptions::InvalidParameterTypeException may be thrown.
|
||||
* To avoid this, use the declare_parameter() method which returns an
|
||||
* rclcpp::ParameterValue instead.
|
||||
*
|
||||
|
|
|
@ -156,12 +156,16 @@ Node::declare_parameter(
|
|||
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
|
||||
bool ignore_override)
|
||||
{
|
||||
try {
|
||||
return this->declare_parameter(
|
||||
name,
|
||||
rclcpp::ParameterValue(default_value),
|
||||
parameter_descriptor,
|
||||
ignore_override
|
||||
).get<ParameterT>();
|
||||
} catch (const ParameterTypeException & ex) {
|
||||
throw exceptions::InvalidParameterTypeException(name, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename ParameterT>
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <vector>
|
||||
|
||||
#include "rcl_interfaces/msg/parameter.hpp"
|
||||
#include "rclcpp/exceptions.hpp"
|
||||
#include "rclcpp/parameter_value.hpp"
|
||||
#include "rclcpp/visibility_control.hpp"
|
||||
|
||||
|
@ -221,8 +222,12 @@ template<typename T>
|
|||
decltype(auto)
|
||||
Parameter::get_value() const
|
||||
{
|
||||
try {
|
||||
// use the helper to specialize for the ParameterValue and Parameter cases.
|
||||
return detail::get_value_helper<T>(this);
|
||||
} catch (const ParameterTypeException & ex) {
|
||||
throw exceptions::InvalidParameterTypeException(this->name_, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rclcpp
|
||||
|
|
|
@ -574,7 +574,7 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
|
|||
// default type and initial value type do not match
|
||||
EXPECT_THROW(
|
||||
{node->declare_parameter("parameter_type_mismatch", 42);},
|
||||
rclcpp::ParameterTypeException);
|
||||
rclcpp::exceptions::InvalidParameterTypeException);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1717,7 +1717,7 @@ TEST_F(TestNode, get_parameter_undeclared_parameters_not_allowed) {
|
|||
int value;
|
||||
node->get_parameter(name, value);
|
||||
},
|
||||
rclcpp::ParameterTypeException);
|
||||
rclcpp::exceptions::InvalidParameterTypeException);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1951,7 +1951,7 @@ TEST_F(TestNode, get_parameters_undeclared_parameters_not_allowed) {
|
|||
{
|
||||
node_local->get_parameters("", actual);
|
||||
},
|
||||
rclcpp::ParameterTypeException);
|
||||
rclcpp::exceptions::InvalidParameterTypeException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue