From 3ab899a9a8bf676463982b858b2738f1fab8bc14 Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Sat, 3 Feb 2018 17:29:59 +0100 Subject: [PATCH] cleanup error handling throw std::logic_error on type errors throw std::runtime_error on undeclared property don't expose generic PropertyMap::declare() --- .../moveit/task_constructor/properties.h | 7 ++++--- core/src/properties.cpp | 17 +++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/core/include/moveit/task_constructor/properties.h b/core/include/moveit/task_constructor/properties.h index 8bc849c9..8555fd32 100644 --- a/core/include/moveit/task_constructor/properties.h +++ b/core/include/moveit/task_constructor/properties.h @@ -118,16 +118,17 @@ class PropertyMap { std::map props_; -public: - /// declare a property for future use + /// implementation of declare methods Property& declare(const std::string& name, const std::type_info& type, const std::string& description = "", const boost::any& default_value = boost::any()); - +public: + /// declare a property for future use template Property& declare(const std::string& name, const std::string& description = "") { return declare(name, typeid(T), description); } + /// declare a property with default value template Property& declare(const std::string& name, const T& default_value, const std::string& description = "") { diff --git a/core/src/properties.cpp b/core/src/properties.cpp index d26ab5b8..7e714d58 100644 --- a/core/src/properties.cpp +++ b/core/src/properties.cpp @@ -38,7 +38,6 @@ #include #include -#include #include namespace moveit { @@ -50,8 +49,8 @@ Property::Property(const std::type_index& type_index, const std::string& descrip , default_(default_value) , value_(default_value) { - if (!default_.empty() && std::type_index(default_.type()) != type_index_) - throw std::runtime_error("type of default value doesn't match declared type"); + // default value's type should match declared type by construction + assert(default_.empty() || std::type_index(default_.type()) == type_index_); } namespace { @@ -59,7 +58,7 @@ void typeCheck(const boost::any& value, const std::type_index& type_index) { if (std::type_index(value.type()) != type_index) { static boost::format fmt("type (%1%) doesn't match property's declared type (%2%)"); - throw std::runtime_error(boost::str(fmt % value.type().name() % type_index.name())); + throw std::logic_error(boost::str(fmt % value.type().name() % type_index.name())); } } } @@ -109,7 +108,7 @@ Property& PropertyMap::declare(const std::string &name, const std::type_info &ty { auto it_inserted = props_.insert(std::make_pair(name, Property(std::type_index(type), description, default_value))); if (!it_inserted.second && std::type_index(type) != it_inserted.first->second.type_index_) - throw std::runtime_error("Property '" + name + "' was already declared with different type."); + throw std::logic_error("Property '" + name + "' was already declared with different type."); return it_inserted.first->second; } @@ -117,7 +116,7 @@ Property& PropertyMap::property(const std::string &name) { auto it = props_.find(name); if (it == props_.end()) - throw std::runtime_error("Unknown property '" + name + "'"); + throw std::runtime_error("Undeclared property '" + name + "'"); return it->second; } @@ -133,10 +132,8 @@ void PropertyMap::set(const std::string &name, const boost::any &value) { auto range = props_.equal_range(name); if (range.first == range.second) { // name is not yet declared - if (value.empty()) { - ROS_ERROR("trying to define property '%s' with NULL value", name.c_str()); - return; - } + if (value.empty()) + throw std::logic_error("trying to set undeclared property '" + name + "' with NULL value"); auto it = props_.insert(range.first, std::make_pair(name, Property(value.type(), "", boost::any()))); it->second.setValue(value); } else {