From b05c6e7aeeb05bd56bf6dba6f667d78c2777787b Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Wed, 18 Mar 2020 10:51:18 -0700 Subject: [PATCH] Add InvalidParameterTypeException Used to wrap the ParameterTypeException coming from ParameterValue::get() for improving the error message. Signed-off-by: Jacob Perron --- rclcpp/include/rclcpp/exceptions.hpp | 15 +++++++++++++++ rclcpp/include/rclcpp/node.hpp | 2 +- rclcpp/include/rclcpp/node_impl.hpp | 16 ++++++++++------ rclcpp/include/rclcpp/parameter.hpp | 9 +++++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/rclcpp/include/rclcpp/exceptions.hpp b/rclcpp/include/rclcpp/exceptions.hpp index 1976902d92..86e908e68e 100644 --- a/rclcpp/include/rclcpp/exceptions.hpp +++ b/rclcpp/include/rclcpp/exceptions.hpp @@ -226,6 +226,21 @@ class InvalidParameterValueException : public std::runtime_error using std::runtime_error::runtime_error; }; +/// Thrown if requested parameter type is invalid. +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 { diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index b9398f567c..43e3a94fdc 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -301,7 +301,7 @@ class Node : public std::enable_shared_from_this * * 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. * diff --git a/rclcpp/include/rclcpp/node_impl.hpp b/rclcpp/include/rclcpp/node_impl.hpp index ab0c8f806f..20692414cf 100644 --- a/rclcpp/include/rclcpp/node_impl.hpp +++ b/rclcpp/include/rclcpp/node_impl.hpp @@ -158,12 +158,16 @@ Node::declare_parameter( const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor, bool ignore_override) { - return this->declare_parameter( - name, - rclcpp::ParameterValue(default_value), - parameter_descriptor, - ignore_override - ).get(); + try { + return this->declare_parameter( + name, + rclcpp::ParameterValue(default_value), + parameter_descriptor, + ignore_override + ).get(); + } catch (const ParameterTypeException & ex) { + throw exceptions::InvalidParameterTypeException(name, ex.what()); + } } template diff --git a/rclcpp/include/rclcpp/parameter.hpp b/rclcpp/include/rclcpp/parameter.hpp index cee5585bea..e01d5aa772 100644 --- a/rclcpp/include/rclcpp/parameter.hpp +++ b/rclcpp/include/rclcpp/parameter.hpp @@ -22,6 +22,7 @@ #include #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 decltype(auto) Parameter::get_value() const { - // use the helper to specialize for the ParameterValue and Parameter cases. - return detail::get_value_helper(this); + try { + // use the helper to specialize for the ParameterValue and Parameter cases. + return detail::get_value_helper(this); + } catch (const ParameterTypeException & ex) { + throw exceptions::InvalidParameterTypeException(this->name_, ex.what()); + } } } // namespace rclcpp