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:
Jacob Perron 2020-04-08 20:58:22 -07:00 committed by GitHub
parent 01a6befdde
commit fd8cfa8fe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 12 deletions

View file

@ -226,6 +226,25 @@ class InvalidParameterValueException : public std::runtime_error
using std::runtime_error::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. /// Thrown if parameter is already declared.
class ParameterAlreadyDeclaredException : public std::runtime_error class ParameterAlreadyDeclaredException : public std::runtime_error
{ {

View file

@ -301,7 +301,7 @@ public:
* *
* If the type of the default value, and therefore also the type of return * 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 * 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 * To avoid this, use the declare_parameter() method which returns an
* rclcpp::ParameterValue instead. * rclcpp::ParameterValue instead.
* *

View file

@ -156,12 +156,16 @@ Node::declare_parameter(
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor, const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
bool ignore_override) bool ignore_override)
{ {
try {
return this->declare_parameter( return this->declare_parameter(
name, name,
rclcpp::ParameterValue(default_value), rclcpp::ParameterValue(default_value),
parameter_descriptor, parameter_descriptor,
ignore_override ignore_override
).get<ParameterT>(); ).get<ParameterT>();
} catch (const ParameterTypeException & ex) {
throw exceptions::InvalidParameterTypeException(name, ex.what());
}
} }
template<typename ParameterT> template<typename ParameterT>

View file

@ -22,6 +22,7 @@
#include <vector> #include <vector>
#include "rcl_interfaces/msg/parameter.hpp" #include "rcl_interfaces/msg/parameter.hpp"
#include "rclcpp/exceptions.hpp"
#include "rclcpp/parameter_value.hpp" #include "rclcpp/parameter_value.hpp"
#include "rclcpp/visibility_control.hpp" #include "rclcpp/visibility_control.hpp"
@ -221,8 +222,12 @@ template<typename T>
decltype(auto) decltype(auto)
Parameter::get_value() const Parameter::get_value() const
{ {
try {
// use the helper to specialize for the ParameterValue and Parameter cases. // use the helper to specialize for the ParameterValue and Parameter cases.
return detail::get_value_helper<T>(this); return detail::get_value_helper<T>(this);
} catch (const ParameterTypeException & ex) {
throw exceptions::InvalidParameterTypeException(this->name_, ex.what());
}
} }
} // namespace rclcpp } // namespace rclcpp

View file

@ -574,7 +574,7 @@ TEST_F(TestNode, declare_parameter_with_overrides) {
// default type and initial value type do not match // default type and initial value type do not match
EXPECT_THROW( EXPECT_THROW(
{node->declare_parameter("parameter_type_mismatch", 42);}, {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; int value;
node->get_parameter(name, 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); node_local->get_parameters("", actual);
}, },
rclcpp::ParameterTypeException); rclcpp::exceptions::InvalidParameterTypeException);
} }
} }
} }