From: Pawel Andruszkiewicz
Date: Mon, 8 Feb 2016 09:20:54 +0000 (+0100)
Subject: [iotcon] Error converter.
X-Git-Tag: submit/tizen/20160212.103506^2~9
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14ae51e0bdc58986f51ce8fdf2bb5a402148f526;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git
[iotcon] Error converter.
Change-Id: I2481ea985c5d0fe030bbb690a8985081a7ecb3a5
Signed-off-by: Pawel Andruszkiewicz
---
diff --git a/src/common/logger.h b/src/common/logger.h
index a807bdb0..8be5a416 100644
--- a/src/common/logger.h
+++ b/src/common/logger.h
@@ -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
diff --git a/src/iotcon/iotcon_instance.cc b/src/iotcon/iotcon_instance.cc
index 5c2e3f62..e32b858b 100644
--- a/src/iotcon/iotcon_instance.cc
+++ b/src/iotcon/iotcon_instance.cc
@@ -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(timeout)}};
@@ -354,10 +354,10 @@ common::TizenResult IotconInstance::SetTimeout(const picojson::object& args) {
CHECK_EXIST(args, "timeout");
int timeout = static_cast(args.find("timeout")->second.get());
+ 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();
diff --git a/src/iotcon/iotcon_server_manager.cc b/src/iotcon/iotcon_server_manager.cc
index 077dccaf..56a917b5 100644
--- a/src/iotcon/iotcon_server_manager.cc
+++ b/src/iotcon/iotcon_server_manager.cc
@@ -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
diff --git a/src/iotcon/iotcon_utils.cc b/src/iotcon/iotcon_utils.cc
index 004b2157..a6560c32 100644
--- a/src/iotcon/iotcon_utils.cc
+++ b/src/iotcon/iotcon_utils.cc
@@ -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()) {
return LogAndCreateTizenError(InvalidValuesError, "Array holds incorrect types");
} else {
- ret = iotcon_resource_types_add(resource_types, iter->get().c_str());
- if (IOTCON_ERROR_NONE != ret) {
+ result = ConvertIotconError(iotcon_resource_types_add(resource_types, iter->get().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
diff --git a/src/iotcon/iotcon_utils.h b/src/iotcon/iotcon_utils.h
index 8a0bbb5d..4c1cac06 100644
--- a/src/iotcon/iotcon_utils.h
+++ b/src/iotcon/iotcon_utils.h
@@ -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