[iotcon] Error converter.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 8 Feb 2016 09:20:54 +0000 (10:20 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 10 Feb 2016 09:01:32 +0000 (18:01 +0900)
Change-Id: I2481ea985c5d0fe030bbb690a8985081a7ecb3a5
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/common/logger.h
src/iotcon/iotcon_instance.cc
src/iotcon/iotcon_server_manager.cc
src/iotcon/iotcon_utils.cc
src/iotcon/iotcon_utils.h

index a807bdb0f841d16ec7e08bbc6946f62f7d33ebb1..8be5a416499d9cbf926336d3550fcc9a5bbd819e 100644 (file)
@@ -223,6 +223,27 @@ public:
      LogAndCreateTizenError_1(__VA_ARGS__) \
   )
 
+// internal macros
+#define LogAndReturnTizenError_1(error) \
+  do { \
+    LoggerE("Reporting error."); \
+    return error; \
+  } while(false)
+
+#define LogAndReturnTizenError_2(error, log) \
+  do { \
+    LoggerE log; \
+    return error; \
+  } while(false)
+
+#define LogAndReturnTizenError_X(_0, _1, _2, FUNC, ...) FUNC
+
+#define LogAndReturnTizenError(...) \
+  LogAndReturnTizenError_X(, ##__VA_ARGS__, \
+     LogAndReturnTizenError_2(__VA_ARGS__), \
+     LogAndReturnTizenError_1(__VA_ARGS__) \
+  )
+
 namespace common {
 
 // defined here, so LoggerD() depends on TIZEN_DEBUG_ENABLE flag on per-module
index 5c2e3f62cbc2cba76a2eaffaeb3eeee0e18173b5..e32b858b8c8cd5cf41c45865ffedb49eff0b7a3d 100644 (file)
@@ -339,10 +339,10 @@ common::TizenResult IotconInstance::GetTimeout(const picojson::object& args) {
   ScopeLogger();
 
   int timeout = 0;
-  int ret = iotcon_get_timeout(&timeout);
+  auto result = IotconUtils::ConvertIotconError(iotcon_get_timeout(&timeout));
 
-  if (IOTCON_ERROR_NONE != ret) {
-    return LogAndCreateTizenError(UnknownError, ret);
+  if (!result) {
+    LogAndReturnTizenError(result);
   }
 
   return common::TizenSuccess{picojson::value{static_cast<double>(timeout)}};
@@ -354,10 +354,10 @@ common::TizenResult IotconInstance::SetTimeout(const picojson::object& args) {
   CHECK_EXIST(args, "timeout");
 
   int timeout = static_cast<int>(args.find("timeout")->second.get<double>());
+  auto result = IotconUtils::ConvertIotconError(iotcon_set_timeout(timeout));
 
-  int ret = iotcon_set_timeout(timeout);
-  if (IOTCON_ERROR_NONE != ret) {
-    return LogAndCreateTizenError(UnknownError, ret);
+  if (!result) {
+    LogAndReturnTizenError(result);
   }
 
   return common::TizenSuccess();
index 077dccaf2edaca0407f2d971d18ff55aaf80ab8b..56a917b5a9ffb5b2327e4fa5e1b49cfc5d9a4946 100644 (file)
@@ -62,9 +62,9 @@ TizenResult IotconServerManager::RestoreHandles() {
                                      nullptr, // user_data
                                      &(resource->handle));
     if (IOTCON_ERROR_NONE != ret || nullptr == resource->handle) {
-      return LogAndCreateTizenError(UnknownError, "Unknown error occurred.",
-                                    ("iotcon_resource_create failed: %d (%s)",
-                                    ret, get_error_message(ret)));
+      LogAndReturnTizenError(IotconUtils::ConvertIotconError(ret),
+                             ("iotcon_resource_create() failed: %d (%s)",
+                                 ret, get_error_message(ret)));
     }
     LoggerD("new handle: %p", (resource->handle));
     if (old_handle) {
@@ -114,9 +114,9 @@ TizenResult IotconServerManager::CreateResource(const std::string& uri_path,
                                nullptr, // user_data
                                &(res_pointer->handle));
   if (IOTCON_ERROR_NONE != ret || nullptr == res_pointer->handle) {
-    return LogAndCreateTizenError(UnknownError, "Unknown error occurred.",
-                                  ("iotcon_resource_create failed: %d (%s)",
-                                  ret, get_error_message(ret)));
+    LogAndReturnTizenError(IotconUtils::ConvertIotconError(ret),
+                           ("iotcon_resource_create() failed: %d (%s)",
+                               ret, get_error_message(ret)));
   }
 
   // storing ResourceInfo into map
index 004b2157a514f5c356433859b06a7bad5544d251..a6560c32a1148621e52c6880d7ee4d5a5b5476b1 100644 (file)
@@ -108,23 +108,19 @@ TizenResult IotconUtils::ArrayToTypes(const picojson::array& types, iotcon_resou
   ScopeLogger();
 
   iotcon_resource_types_h resource_types = nullptr;
-  int ret = iotcon_resource_types_create(&resource_types);
-  if (IOTCON_ERROR_NONE != ret) {
-    return LogAndCreateTizenError(UnknownError, "Unknown error occurred.",
-                                  ("iotcon_resource_types_create failed: %d (%s)",
-                                  ret, get_error_message(ret)));
+  auto result = ConvertIotconError(iotcon_resource_types_create(&resource_types));
+  if (!result) {
+    LogAndReturnTizenError(result, ("iotcon_resource_types_create() failed"));
   }
 
   for (auto iter = types.begin(); iter != types.end(); ++iter) {
     if (!iter->is<std::string>()) {
       return LogAndCreateTizenError(InvalidValuesError, "Array holds incorrect types");
     } else {
-      ret = iotcon_resource_types_add(resource_types, iter->get<std::string>().c_str());
-      if (IOTCON_ERROR_NONE != ret) {
+      result = ConvertIotconError(iotcon_resource_types_add(resource_types, iter->get<std::string>().c_str()));
+      if (!result) {
         iotcon_resource_types_destroy(resource_types);
-        return LogAndCreateTizenError(UnknownError, "Unknown error occurred.",
-                                      ("iotcon_resource_types_add failed: %d (%s)",
-                                      ret, get_error_message(ret)));
+        LogAndReturnTizenError(result, ("iotcon_resource_types_add() failed"));
       }
     }
   }
@@ -152,28 +148,24 @@ TizenResult IotconUtils::ExtractFromResource(const ResourceInfoPtr& pointer,
                                              int* properties) {
   ScopeLogger();
 
-  int ret = iotcon_resource_get_uri_path (pointer->handle, uri_path);
-  if (IOTCON_ERROR_NONE != ret) {
-    LoggerD("Error %s", get_error_message(ret));
-    return LogAndCreateTizenError(UnknownError, "Gathering resource uri path failed");
+  auto result = ConvertIotconError(iotcon_resource_get_uri_path(pointer->handle, uri_path));
+  if (!result) {
+    LogAndReturnTizenError(result, ("Gathering resource uri path failed"));
   }
 
-  ret = iotcon_resource_get_types (pointer->handle, res_types);
-  if (IOTCON_ERROR_NONE != ret) {
-    LoggerD("Error %s", get_error_message(ret));
-    return LogAndCreateTizenError(UnknownError, "Gathering resource types failed");
+  result = ConvertIotconError(iotcon_resource_get_types(pointer->handle, res_types));
+  if (!result) {
+    LogAndReturnTizenError(result, ("Gathering resource types failed"));
   }
 
-  ret = iotcon_resource_get_interfaces (pointer->handle, ifaces);
-  if (IOTCON_ERROR_NONE != ret) {
-    LoggerD("Error %s", get_error_message(ret));
-    return LogAndCreateTizenError(UnknownError, "Gathering resource interfaces failed");
+  result = ConvertIotconError(iotcon_resource_get_interfaces(pointer->handle, ifaces));
+  if (!result) {
+    LogAndReturnTizenError(result, ("Gathering resource interfaces failed"));
   }
 
-  ret = iotcon_resource_get_properties (pointer->handle, properties);
-  if (IOTCON_ERROR_NONE != ret) {
-    LoggerD("Error %s", get_error_message(ret));
-    return LogAndCreateTizenError(UnknownError, "Gathering resource properties failed");
+  result = ConvertIotconError(iotcon_resource_get_properties(pointer->handle, properties));
+  if (!result) {
+    LogAndReturnTizenError(result, ("Gathering resource properties failed"));
   }
   return TizenSuccess();
 }
@@ -239,5 +231,44 @@ TizenResult IotconUtils::ResourceToJson(ResourceInfoPtr pointer,
   return TizenSuccess();
 }
 
+common::TizenResult IotconUtils::ConvertIotconError(int error) {
+  switch (error) {
+    case IOTCON_ERROR_NONE:
+      return common::TizenSuccess();
+
+    case IOTCON_ERROR_IO_ERROR:
+      return common::IoError(error);
+
+    case IOTCON_ERROR_PERMISSION_DENIED:
+      return common::SecurityError(error);
+
+    case IOTCON_ERROR_NOT_SUPPORTED:
+      return common::NotSupportedError(error);
+
+    case IOTCON_ERROR_INVALID_PARAMETER:
+      return common::InvalidValuesError(error);
+
+    case IOTCON_ERROR_NO_DATA:
+      return common::NotFoundError(error);
+
+    case IOTCON_ERROR_TIMEOUT:
+      return common::TimeoutError(error);
+
+    case IOTCON_ERROR_INVALID_TYPE:
+      return common::TypeMismatchError(error);
+
+    case IOTCON_ERROR_ALREADY:
+      return common::InvalidStateError(error);
+
+    case IOTCON_ERROR_OUT_OF_MEMORY:
+    case IOTCON_ERROR_IOTIVITY:
+    case IOTCON_ERROR_REPRESENTATION:
+    case IOTCON_ERROR_DBUS:
+    case IOTCON_ERROR_SYSTEM:
+    default:
+      return common::AbortError(error);
+  }
+}
+
 } // namespace iotcon
 } // namespace extension
index 8a0bbb5d0057a886977daf4c85b65f2ae7f89ee5..4c1cac068b043e0964ba027b27a3be9c01de0eff 100644 (file)
@@ -72,6 +72,8 @@ class IotconUtils {
   static common::TizenResult ResourceToJson(ResourceInfoPtr pointer,
                                             const IotconServerManager& manager,
                                             picojson::object* res);
+
+  static common::TizenResult ConvertIotconError(int error);
 };
 
 } // namespace iotcon