[contact][Calendar] Removed try/catch error handling - filter utils
authorRyszard Matuszyk <r.matuszyk@samsung.com>
Tue, 24 Feb 2015 15:05:46 +0000 (16:05 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Wed, 25 Feb 2015 08:55:41 +0000 (17:55 +0900)
Verification] Build required

Change-Id: I95246ec39e03b1fdf6fa549750162fff17a6ed55
Signed-off-by: Ryszard Matuszyk <r.matuszyk@samsung.com>
src/calendar/calendar.cc
src/common/filter-utils.cc
src/common/filter-utils.h
src/contact/contact_manager.cc

index 2a8ecec39a82629436783221ddab8a6219d71d6f..a3e68fec5b9d032b7a247716798f8266e497ff3b 100644 (file)
@@ -440,9 +440,13 @@ void Calendar::Find(const picojson::object& args, picojson::array& array) {
       }
       intermediate_filters[intermediate_filters.size() - 1].push_back(
           std::move(calendar_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
     visitor.SetOnCompositeFilterBegin([&](CompositeFilterType type) {
       intermediate_filters.push_back(std::vector<CalendarFilterPtr>());
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.SetOnCompositeFilterEnd([&](CompositeFilterType calType) {
@@ -479,6 +483,8 @@ void Calendar::Find(const picojson::object& args, picojson::array& array) {
       }
       intermediate_filters.pop_back();
       intermediate_filters.back().push_back(std::move(merged_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.SetOnAttributeRangeFilter([&](const std::string& name,
@@ -661,6 +667,8 @@ void Calendar::Find(const picojson::object& args, picojson::array& array) {
       }
       intermediate_filters[intermediate_filters.size() - 1].push_back(
           std::move(calendar_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
     visitor.Visit(FromJson<JsonObject>(args, "filter"));
     if ((intermediate_filters.size() != 1) ||
index 358e0ef2a3da2325a90e114b4745dac654d0196d..911cfbe472bcc0dd7e5e7d8d55dc6a103180eb6a 100644 (file)
 
 namespace common {
 
-AttributeMatchFlag AttributeMatchFlagFromString(const std::string &str) {
-    if (str == "EXACTLY") {
-        return AttributeMatchFlag::kExactly;
-    }
-    if (str == "FULLSTRING") {
-        return AttributeMatchFlag::kFullString;
-    }
-    if (str == "CONTAINS") {
-        return AttributeMatchFlag::kContains;
-    }
-    if (str == "STARTSWITH") {
-        return AttributeMatchFlag::kStartsWith;
-    }
-    if (str == "ENDSWITH") {
-        return AttributeMatchFlag::kEndsWith;
-    }
-    if (str == "EXISTS") {
-        return AttributeMatchFlag::kExists;
-    }
-
+PlatformResult AttributeMatchFlagFromString(
+    const std::string &str, AttributeMatchFlag *filter_match_flag) {
+  if (str == "EXACTLY") {
+    *filter_match_flag = AttributeMatchFlag::kExactly;
+  } else if (str == "FULLSTRING") {
+    *filter_match_flag = AttributeMatchFlag::kFullString;
+  } else if (str == "CONTAINS") {
+    *filter_match_flag = AttributeMatchFlag::kContains;
+  } else if (str == "STARTSWITH") {
+    *filter_match_flag = AttributeMatchFlag::kStartsWith;
+  } else if (str == "ENDSWITH") {
+    *filter_match_flag = AttributeMatchFlag::kEndsWith;
+  } else if (str == "EXISTS") {
+    *filter_match_flag = AttributeMatchFlag::kExists;
+  } else {
     LoggerE("Invalid attribute match string: %i", str.c_str());
+    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+                          "Invalid attribute match string!");
+  }
 
-    throw InvalidValuesException("Invalid attribute match string!");
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-CompositeFilterType CompositeFilterTypeFromString(const std::string &str) {
-    if (str == "UNION") {
-        return CompositeFilterType::kUnion;
-    }
-    if (str == "INTERSECTION") {
-        return CompositeFilterType::kIntersection;
-    }
-
+PlatformResult CompositeFilterTypeFromString(
+    const std::string &str, CompositeFilterType *comp_filter_type) {
+  if (str == "UNION") {
+    *comp_filter_type = CompositeFilterType::kUnion;
+  } else if (str == "INTERSECTION") {
+    *comp_filter_type = CompositeFilterType::kIntersection;
+  } else {
     LoggerE("Invalid composite type string: %i", str.c_str());
 
-    throw InvalidValuesException("Invalid composite type string!");
+    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+                          "Invalid composite type string!");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
 void FilterVisitor::SetOnAttributeFilter(const AttributeFilterOnVisit &func) {
@@ -75,56 +76,87 @@ void FilterVisitor::SetOnCompositeFilterEnd(const CompositeFilterOnEnd &func) {
     m_compositeFilterOnEnd = func;
 }
 
-void FilterVisitor::Visit(const picojson::object &filter) {
-    const std::string& filterType = FromJson<std::string>(filter, "filterType");
-    if (filterType == "AttributeFilter") {
-        VisitAttributeFilter(filter);
-    } else if (filterType == "AttributeRangeFilter") {
-        VisitAttributeRangeFilter(filter);
-    } else if (filterType == "CompositeFilter") {
-        VisitCompositeFilter(filter);
-    } else {
-        throw InvalidValuesException("Invalid filter type!");
-    }
+PlatformResult FilterVisitor::Visit(const picojson::object &filter) {
+  const std::string &filterType = FromJson<std::string>(filter, "filterType");
+  if (filterType == "AttributeFilter") {
+    PlatformResult status = VisitAttributeFilter(filter);
+    if (status.IsError()) return status;
+  } else if (filterType == "AttributeRangeFilter") {
+    PlatformResult status = VisitAttributeRangeFilter(filter);
+    if (status.IsError()) return status;
+  } else if (filterType == "CompositeFilter") {
+    PlatformResult status = VisitCompositeFilter(filter);
+    if (status.IsError()) return status;
+  } else {
+    LoggerE("Invalid filter type!");
+    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+                          "Invalid filter type!");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-void FilterVisitor::VisitAttributeFilter(const picojson::object &filter) {
-    const std::string& attributeName = FromJson<std::string>(filter, "attributeName");
-    AttributeMatchFlag matchFlag =
-            AttributeMatchFlagFromString(FromJson<std::string>(filter, "matchFlag"));
-    const picojson::value& matchValue = FindValue(filter, "matchValue");
+PlatformResult FilterVisitor::VisitAttributeFilter(
+    const picojson::object &filter) {
+  const std::string &attribute_name =
+      FromJson<std::string>(filter, "attributeName");
+
+  AttributeMatchFlag match_flag;
+  PlatformResult status = AttributeMatchFlagFromString(
+      FromJson<std::string>(filter, "matchFlag"), &match_flag);
+  if (status.IsError()) return status;
+
+  const picojson::value &match_value = FindValue(filter, "matchValue");
 
-    if (m_attributeFilterOnVisit) {
-        m_attributeFilterOnVisit(attributeName, matchFlag, matchValue);
-    }
+  if (m_attributeFilterOnVisit) {
+    status = m_attributeFilterOnVisit(attribute_name, match_flag, match_value);
+    if (status.IsError()) return status;
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-void FilterVisitor::VisitAttributeRangeFilter(const picojson::object &filter) {
-    const std::string& attributeName = FromJson<std::string>(filter, "attributeName");
-    const picojson::value& initialValue = FindValue(filter, "initialValue");
-    const picojson::value& endValue = FindValue(filter, "endValue");
+PlatformResult FilterVisitor::VisitAttributeRangeFilter(
+    const picojson::object &filter) {
+  const std::string &attributeName =
+      FromJson<std::string>(filter, "attributeName");
+  const picojson::value &initialValue = FindValue(filter, "initialValue");
+  const picojson::value &endValue = FindValue(filter, "endValue");
 
-    if (m_attributeRangeFilterOnVisit) {
+  if (m_attributeRangeFilterOnVisit) {
+    PlatformResult status =
         m_attributeRangeFilterOnVisit(attributeName, initialValue, endValue);
-    }
+    if (status.IsError()) return status;
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-void FilterVisitor::VisitCompositeFilter(const picojson::object &filter) {
-    CompositeFilterType filterType =
-            CompositeFilterTypeFromString(FromJson<std::string>(filter, "type"));
-    const picojson::array& filters = FromJson<picojson::array>(filter, "filters");
+PlatformResult FilterVisitor::VisitCompositeFilter(
+    const picojson::object &filter) {
+  CompositeFilterType filter_type;
+  PlatformResult status = CompositeFilterTypeFromString(
+      FromJson<std::string>(filter, "type"), &filter_type);
+  if (status.IsError()) return status;
+
+  const picojson::array &filters = FromJson<picojson::array>(filter, "filters");
+
+  if (m_compositeFilterOnBegin) {
+    status = m_compositeFilterOnBegin(filter_type);
+    if (status.IsError()) return status;
+  }
 
-    if (m_compositeFilterOnBegin) {
-        m_compositeFilterOnBegin(filterType);
-    }
+  for (std::size_t i = 0; i < filters.size(); ++i) {
+    PlatformResult status = Visit(JsonCast<picojson::object>(filters[i]));
+    if (status.IsError()) return status;
+  }
 
-    for (std::size_t i = 0; i < filters.size(); ++i) {
-        Visit(JsonCast<picojson::object>(filters[i]));
-    }
+  if (m_compositeFilterOnEnd) {
+    status = m_compositeFilterOnEnd(filter_type);
+    if (status.IsError()) return status;
+  }
 
-    if (m_compositeFilterOnEnd) {
-        m_compositeFilterOnEnd(filterType);
-    }
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
 }
index 003524fbfeb08dc672405e13b07a014321d9b09f..8d9d63333ae1cb2af824411a10d91c7fd3fd19ab 100644 (file)
@@ -21,6 +21,7 @@
 #include <memory>
 
 #include "picojson.h"
+#include "platform_result.h"
 
 namespace common {
 
@@ -40,24 +41,29 @@ enum class AttributeMatchFlag {
     kExists
 };
 
-AttributeMatchFlag AttributeMatchFlagFromString(const std::string& str);
+PlatformResult AttributeMatchFlagFromString(
+    const std::string& str, AttributeMatchFlag* filter_match_flag);
 
 enum class CompositeFilterType {
     kUnion,
     kIntersection
 };
 
-CompositeFilterType CompositeFilterTypeFromString(const std::string& str);
+PlatformResult CompositeFilterTypeFromString(
+    const std::string& str, CompositeFilterType* comp_filter_type);
 
-typedef std::function<void(const std::string&, AttributeMatchFlag, const picojson::value&)>
-        AttributeFilterOnVisit;
+typedef std::function<PlatformResult(const std::string&, AttributeMatchFlag,
+                                     const picojson::value&)>
+    AttributeFilterOnVisit;
 
-typedef std::function<void(const std::string&, const picojson::value&, const picojson::value&)>
-        AttributeRangeFilterOnVisit;
+typedef std::function<PlatformResult(const std::string&, const picojson::value&,
+                                     const picojson::value&)>
+    AttributeRangeFilterOnVisit;
 
-typedef std::function<void(CompositeFilterType)> CompositeFilterOnBegin;
+typedef std::function<PlatformResult(CompositeFilterType)>
+    CompositeFilterOnBegin;
 
-typedef std::function<void(CompositeFilterType)> CompositeFilterOnEnd;
+typedef std::function<PlatformResult(CompositeFilterType)> CompositeFilterOnEnd;
 
 /**
  * @brief The FilterVisitor class
@@ -105,12 +111,12 @@ class FilterVisitor {
      * @brief Parses a json object as Tizen filter.
      * @param filter Object to be visited
      */
-    void Visit(const picojson::object& filter);
+    PlatformResult Visit(const picojson::object& filter);
 
 private:
-    void VisitAttributeFilter(const picojson::object& filter);
-    void VisitAttributeRangeFilter(const picojson::object& filter);
-    void VisitCompositeFilter(const picojson::object& filter);
+    PlatformResult VisitAttributeFilter(const picojson::object& filter);
+    PlatformResult VisitAttributeRangeFilter(const picojson::object& filter);
+    PlatformResult VisitCompositeFilter(const picojson::object& filter);
 
     AttributeFilterOnVisit m_attributeFilterOnVisit;
     AttributeRangeFilterOnVisit m_attributeRangeFilterOnVisit;
index 201ad2139f844fecf3f8550a8834c864bb2d30e2..1fa6ce98e2c87f41e059cf96918369d63a9b0602 100644 (file)
@@ -375,6 +375,8 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) {
       }
       intermediate_filters[intermediate_filters.size() - 1]
           .push_back(std::move(contacts_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.SetOnAttributeRangeFilter([&](const std::string& name,
@@ -553,11 +555,15 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) {
       }
       intermediate_filters[intermediate_filters.size() - 1]
           .push_back(std::move(contacts_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.SetOnCompositeFilterBegin([&](CompositeFilterType type) {
       intermediate_filters.push_back(
           std::vector<ContactUtil::ContactsFilterPtr>());
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.SetOnCompositeFilterEnd([&](CompositeFilterType type) {
@@ -594,6 +600,8 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) {
 
       intermediate_filters.pop_back();
       intermediate_filters.back().push_back(std::move(merged_filter_ptr));
+
+      return PlatformResult(ErrorCode::NO_ERROR);
     });
 
     visitor.Visit(FromJson<JsonObject>(args, "filter"));