From aeb2f601bb6ff5f493cef2964bd7deff595a941c Mon Sep 17 00:00:00 2001 From: Grzegorz Rynkowski Date: Tue, 10 Feb 2015 11:54:52 +0100 Subject: [PATCH] [common] Provided PlatformResult to wrap platform error Description: - Provided PlatformResult class that wraps an error and a optional message - Changed value of NO_ERROR to 0, as it is in C/C++ convention of return value. - Moved error codes to common scope, to avoid circular referencing between tizen and common Verification: Build the code. Change-Id: I0eeef2bcd205cab13c210cc7dc7eb653b5912924 Signed-off-by: Grzegorz Rynkowski --- src/application/application.cc | 11 ++-- src/common/common.gypi | 2 + src/common/extension.cc | 18 +++++++ src/common/extension.h | 6 ++- src/common/platform_result.cc | 25 +++++++++ src/common/platform_result.h | 80 +++++++++++++++++++++++++++++ src/datasync/datasync_manager.cc | 13 +++-- src/datasync/datasync_manager.h | 6 ++- src/tizen/tizen.h | 44 ---------------- src/websetting/websetting.cc | 11 ++-- src/websetting/websetting_extension_utils.h | 3 +- 11 files changed, 155 insertions(+), 64 deletions(-) create mode 100644 src/common/platform_result.cc create mode 100644 src/common/platform_result.h diff --git a/src/application/application.cc b/src/application/application.cc index 85919d2..4443db2 100644 --- a/src/application/application.cc +++ b/src/application/application.cc @@ -9,7 +9,10 @@ #include "common/extension.h" #include "common/logger.h" #include "common/picojson.h" -#include "tizen/tizen.h" + +#include "common/platform_result.h" + +using common::ErrorCode; namespace extension { namespace application { @@ -44,10 +47,9 @@ void Application::set_app_info(const ApplicationInformationPtr& app_info) { const picojson::value& Application::Value() { if (!app_info_->IsValid()) { - LoggerD("WebApiAPIErrors::UNKNOWN_ERR"); + LoggerD("ErrorCode::UNKNOWN_ERR"); picojson::object obj; - obj["error"] = - picojson::value(static_cast(WebApiAPIErrors::UNKNOWN_ERR)); + obj["error"] = picojson::value(static_cast(ErrorCode::UNKNOWN_ERR)); value_ = picojson::value(obj); } else { picojson::object obj; @@ -59,6 +61,5 @@ const picojson::value& Application::Value() { return value_; } - } // namespace application } // namespace extension diff --git a/src/common/common.gypi b/src/common/common.gypi index 7fa3d66..e9943ba 100644 --- a/src/common/common.gypi +++ b/src/common/common.gypi @@ -99,6 +99,8 @@ 'callback_user_data.h', #'multi_callback_user_data.cc', #'multi_callback_user_data.h', + 'platform_result.cc', + 'platform_result.h' ], 'cflags': [ '-std=c++0x', diff --git a/src/common/extension.cc b/src/common/extension.cc index 38296d4..de14fd8 100644 --- a/src/common/extension.cc +++ b/src/common/extension.cc @@ -10,6 +10,8 @@ #include #include +#include "common/logger.h" + namespace { common::Extension* g_extension = NULL; @@ -242,6 +244,10 @@ void ParsedInstance::ReportError(const PlatformException& ex, picojson::object& tools::ReportError(ex, out); } +void ParsedInstance::ReportError(const PlatformResult& error, picojson::object* out) { + tools::ReportError(error, out); +} + void ParsedInstance::HandleMessage(const char* msg) { HandleMessage(msg, false); } @@ -306,6 +312,13 @@ void ParsedInstance::HandleException(const PlatformException& ex) { SendSyncReply(result.serialize().c_str()); } +void ParsedInstance::HandleError(const PlatformResult& e) { + LoggerE("Error: %s", static_cast(e.error_code())); + picojson::value result = picojson::value(picojson::object()); + ReportError(e, &result.get()); + SendSyncReply(result.serialize().c_str()); +} + namespace tools { void ReportSuccess(picojson::object& out) { out.insert(std::make_pair("status", picojson::value("success"))); @@ -324,6 +337,11 @@ void ReportError(const PlatformException& ex, picojson::object& out) { out.insert(std::make_pair("status", picojson::value("error"))); out.insert(std::make_pair("error", ex.ToJSON())); } + +void ReportError(const PlatformResult& error, picojson::object* out) { + out->insert(std::make_pair("status", picojson::value("error"))); + out->insert(std::make_pair("error", error.ToJSON())); +} } // namespace tools } // namespace common diff --git a/src/common/extension.h b/src/common/extension.h index 080c7e8..2396b6a 100644 --- a/src/common/extension.h +++ b/src/common/extension.h @@ -22,12 +22,13 @@ #include #include +#include "common/platform_exception.h" +#include "common/platform_result.h" #include "common/XW_Extension.h" #include "common/XW_Extension_EntryPoints.h" #include "common/XW_Extension_Permissions.h" #include "common/XW_Extension_Runtime.h" #include "common/XW_Extension_SyncMessage.h" -#include "common/platform_exception.h" namespace common { @@ -110,6 +111,7 @@ class ParsedInstance : public Instance { void ReportSuccess(const picojson::value& result, picojson::object& out); void ReportError(picojson::object& out); void ReportError(const PlatformException& ex, picojson::object& out); + void ReportError(const PlatformResult& error, picojson::object* out); private: void HandleMessage(const char* msg); @@ -117,6 +119,7 @@ class ParsedInstance : public Instance { void HandleMessage(const char* msg, bool is_sync); void HandleException(const PlatformException& ex); + void HandleError(const PlatformResult& error); std::map handler_map_; }; @@ -126,6 +129,7 @@ void ReportSuccess(picojson::object& out); void ReportSuccess(const picojson::value& result, picojson::object& out); void ReportError(picojson::object& out); void ReportError(const PlatformException& ex, picojson::object& out); +void ReportError(const PlatformResult& error, picojson::object* out); } // namespace tools } // namespace common diff --git a/src/common/platform_result.cc b/src/common/platform_result.cc new file mode 100644 index 0000000..9e5f1e4 --- /dev/null +++ b/src/common/platform_result.cc @@ -0,0 +1,25 @@ +// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "common/platform_result.h" + +namespace common { + +PlatformResult::PlatformResult(const ErrorCode& error_code, + const std::string& message) + : error_code_(error_code), message_(message) { +} + +picojson::value PlatformResult::ToJSON() const { + picojson::value::object obj; + obj["code"] = picojson::value(static_cast(error_code_)); + if (!message_.empty()) + obj["message"] = picojson::value(message_); + picojson::value ret(obj); + return ret; +} + +} // namespace common + + diff --git a/src/common/platform_result.h b/src/common/platform_result.h new file mode 100644 index 0000000..d52cc3b --- /dev/null +++ b/src/common/platform_result.h @@ -0,0 +1,80 @@ +// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SRC_COMMON_PLATFORM_RESULT_H_ +#define SRC_COMMON_PLATFORM_RESULT_H_ + +#include + +#include "common/picojson.h" + +namespace common { + +// WARNING! This list should be in sync with the equivalent list +// located at tizen/tizen_api.js. Remember to update tizen/tizen_api.js if you +// change something here. +enum class ErrorCode : int { + // NO_ERROR is not really a valid error, but can be used + // to indicate that no error occoured instead of having an + // extra field in the message protocol just for that. + NO_ERROR = 0, + + INDEX_SIZE_ERR = 1, + DOMSTRING_SIZE_ERR = 2, + HIERARCHY_REQUEST_ERR = 3, + WRONG_DOCUMENT_ERR = 4, + INVALID_CHARACTER_ERR = 5, + NO_DATA_ALLOWED_ERR = 6, + NO_MODIFICATION_ALLOWED_ERR = 7, + NOT_FOUND_ERR = 8, + NOT_SUPPORTED_ERR = 9, + INUSE_ATTRIBUTE_ERR = 10, + INVALID_STATE_ERR = 11, + SYNTAX_ERR = 12, + INVALID_MODIFICATION_ERR = 13, + NAMESPACE_ERR = 14, + INVALID_ACCESS_ERR = 15, + VALIDATION_ERR = 16, + TYPE_MISMATCH_ERR = 17, + SECURITY_ERR = 18, + NETWORK_ERR = 19, + ABORT_ERR = 20, + URL_MISMATCH_ERR = 21, + QUOTA_EXCEEDED_ERR = 22, + TIMEOUT_ERR = 23, + INVALID_NODE_TYPE_ERR = 24, + DATA_CLONE_ERR = 25, + + // Error codes for these errors are not really defined anywhere. + INVALID_VALUES_ERR = 100, + IO_ERR = 101, + PERMISSION_DENIED_ERR = 102, + SERVICE_NOT_AVAILABLE_ERR = 103, + DATABASE_ERR = 104, + + UNKNOWN_ERR = -1 +}; + +class PlatformResult { + public: + explicit PlatformResult(const ErrorCode& error_code, + const std::string& message = ""); + + ErrorCode error_code() const { return error_code_; } + std::string message() const { return message_; } + + bool IsSuccess() const { return error_code() == ErrorCode::NO_ERROR; } + bool IsError() const { return !IsSuccess(); } + explicit operator bool() const { return IsSuccess(); } + + picojson::value ToJSON() const; + + protected: + ErrorCode error_code_; + std::string message_; +}; + +} // namespace common + +#endif // SRC_COMMON_PLATFORM_RESULT_H_ diff --git a/src/datasync/datasync_manager.cc b/src/datasync/datasync_manager.cc index db0306e..b2a599c 100644 --- a/src/datasync/datasync_manager.cc +++ b/src/datasync/datasync_manager.cc @@ -11,8 +11,6 @@ #include "common/logger.h" #include "common/converter.h" -#include "tizen/tizen.h" - namespace extension { namespace datasync { @@ -536,7 +534,8 @@ void DataSyncManager::GetProfileId(sync_agent_event_data_s* request, std::string g_free(profile_dir_name); } -void DataSyncManager::Failed(picojson::object& response_obj, picojson::object& answer_obj, int code, +void DataSyncManager::Failed(picojson::object& response_obj, picojson::object& answer_obj, + const common::ErrorCode& code, const std::string& name, const std::string& message) { LoggerE("%s", message.c_str()); response_obj["callback_name"] = picojson::value("onfailed"); @@ -589,15 +588,15 @@ int DataSyncManager::StateChangedCallback(sync_agent_event_data_s* request) { callbacks_.erase(it); if (!progress) { - Failed(response_obj, answer_obj, UNKNOWN_ERR, "Exception", "nullptr status"); + Failed(response_obj, answer_obj, ErrorCode::UNKNOWN_ERR, "Exception", "nullptr status"); } else if (0 == strncmp(progress, "DONE", 4)) { response_obj["callback_name"] = picojson::value("oncompleted"); } else if (0 == strncmp(progress, "CANCEL", 6)) { response_obj["callback_name"] = picojson::value("onstopped"); } else if (0 == strncmp(progress, "ERROR", 5)) { - Failed(response_obj, answer_obj, UNKNOWN_ERR, "Exception", "Datasync failed"); + Failed(response_obj, answer_obj, ErrorCode::UNKNOWN_ERR, "Exception", "Datasync failed"); } else { - Failed(response_obj, answer_obj, UNKNOWN_ERR, "Exception", "Undefined status"); + Failed(response_obj, answer_obj, ErrorCode::UNKNOWN_ERR, "Exception", "Undefined status"); } } @@ -651,7 +650,7 @@ int DataSyncManager::ProgressCallback(sync_agent_event_data_s* request) { answer_obj["totalPerService"] = picojson::value(static_cast(total_per_db)); answer_obj["syncedPerService"] = picojson::value(static_cast(synced_per_db)); } else { - Failed(response_obj, answer_obj, UNKNOWN_ERR, "Exception", "Wrong service type"); + Failed(response_obj, answer_obj, ErrorCode::UNKNOWN_ERR, "Exception", "Wrong service type"); } } diff --git a/src/datasync/datasync_manager.h b/src/datasync/datasync_manager.h index 0071b51..ee79f5c 100644 --- a/src/datasync/datasync_manager.h +++ b/src/datasync/datasync_manager.h @@ -14,6 +14,7 @@ #include "common/utils.h" #include "common/picojson.h" +#include "common/platform_result.h" namespace extension { namespace datasync { @@ -53,8 +54,9 @@ class DataSyncManager { void Item(ds_profile_h* profile_h, const picojson::object& args); void Item(const std::string& id, ds_profile_h* profile_h, picojson::object& out); void GetProfileId(sync_agent_event_data_s* request, std::string& profile_id); - void Failed(picojson::object& response_obj, picojson::object& answer_obj, int code, - const std::string& name, const std::string& message); + void Failed(picojson::object& response_obj, picojson::object& answer_obj, + const common::ErrorCode& code, const std::string& name, + const std::string& message); inline void PrepareResponseObj(const std::string& profile_id, picojson::value& response, picojson::object& response_obj, picojson::value& answer, picojson::object& answer_obj); diff --git a/src/tizen/tizen.h b/src/tizen/tizen.h index ba805e9..d898e2e 100644 --- a/src/tizen/tizen.h +++ b/src/tizen/tizen.h @@ -5,50 +5,6 @@ #ifndef TIZEN_TIZEN_H_ #define TIZEN_TIZEN_H_ -// WARNING! This list should be in sync with the equivalent list -// located at tizen_api.js. Remember to update tizen_api.js if you -// change something here. -enum WebApiAPIErrors { - // NO_ERROR is not really a valid error, but can be used - // to indicate that no error occoured instead of having an - // extra field in the message protocol just for that. - NO_ERROR = -1, - - UNKNOWN_ERR = 0, - INDEX_SIZE_ERR = 1, - DOMSTRING_SIZE_ERR = 2, - HIERARCHY_REQUEST_ERR = 3, - WRONG_DOCUMENT_ERR = 4, - INVALID_CHARACTER_ERR = 5, - NO_DATA_ALLOWED_ERR = 6, - NO_MODIFICATION_ALLOWED_ERR = 7, - NOT_FOUND_ERR = 8, - NOT_SUPPORTED_ERR = 9, - INUSE_ATTRIBUTE_ERR = 10, - INVALID_STATE_ERR = 11, - SYNTAX_ERR = 12, - INVALID_MODIFICATION_ERR = 13, - NAMESPACE_ERR = 14, - INVALID_ACCESS_ERR = 15, - VALIDATION_ERR = 16, - TYPE_MISMATCH_ERR = 17, - SECURITY_ERR = 18, - NETWORK_ERR = 19, - ABORT_ERR = 20, - URL_MISMATCH_ERR = 21, - QUOTA_EXCEEDED_ERR = 22, - TIMEOUT_ERR = 23, - INVALID_NODE_TYPE_ERR = 24, - DATA_CLONE_ERR = 25, - - // Error codes for these errors are not really defined anywhere. - INVALID_VALUES_ERR = 100, - IO_ERR = 101, - PERMISSION_DENIED_ERR = 102, - SERVICE_NOT_AVAILABLE_ERR = 103, - DATABASE_ERR = 104, -}; - #define STR_MATCH_EXACTLY "EXACTLY" #define STR_MATCH_FULLSTRING "FULLSTRING" #define STR_MATCH_CONTAINS "CONTAINS" diff --git a/src/websetting/websetting.cc b/src/websetting/websetting.cc index 58b008b..f51dbcb 100644 --- a/src/websetting/websetting.cc +++ b/src/websetting/websetting.cc @@ -8,8 +8,11 @@ #include #include +#include "common/platform_result.h" #include "websetting/websetting_extension_utils.h" +using common::ErrorCode; + namespace { const char kRuntimeServiceName[] = "org.crosswalkproject.Runtime1"; @@ -56,7 +59,7 @@ WebSetting::~WebSetting() { std::unique_ptr WebSetting::RemoveAllCookies() { if (!running_app_proxy_) { if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_))) - return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR); + return CreateResultMessage(ErrorCode::UNKNOWN_ERR); } GError* error = NULL; GVariant* result = g_dbus_proxy_call_sync(running_app_proxy_, "RemoveAllCookies", NULL, @@ -64,7 +67,7 @@ std::unique_ptr WebSetting::RemoveAllCookies() { if (!result) { std::cerr << "Fail to call 'RemoveuserAgentAllCookies':" << error->message << std::endl; g_error_free(error); - return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR); + return CreateResultMessage(ErrorCode::UNKNOWN_ERR); } return CreateResultMessage(); } @@ -72,7 +75,7 @@ std::unique_ptr WebSetting::RemoveAllCookies() { std::unique_ptr WebSetting::SetUserAgentString(const std::string& user_agent) { if (!running_app_proxy_) { if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_))) - return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR); + return CreateResultMessage(ErrorCode::UNKNOWN_ERR); } GError* error = NULL; GVariant* result = g_dbus_proxy_call_sync(running_app_proxy_, "SetUserAgentString", @@ -81,7 +84,7 @@ std::unique_ptr WebSetting::SetUserAgentString(const std::strin if (!result) { std::cerr << "Fail to call 'SetUserAgentString':" << error->message << std::endl; g_error_free(error); - return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR); + return CreateResultMessage(ErrorCode::UNKNOWN_ERR); } return CreateResultMessage(); } diff --git a/src/websetting/websetting_extension_utils.h b/src/websetting/websetting_extension_utils.h index bfb41f6..a859284 100644 --- a/src/websetting/websetting_extension_utils.h +++ b/src/websetting/websetting_extension_utils.h @@ -8,6 +8,7 @@ #include #include "common/picojson.h" +#include "common/platform_result.h" #include "tizen/tizen.h" std::unique_ptr CreateResultMessage() { @@ -16,7 +17,7 @@ std::unique_ptr CreateResultMessage() { return std::unique_ptr(new picojson::value(obj)); } -std::unique_ptr CreateResultMessage(WebApiAPIErrors error) { +std::unique_ptr CreateResultMessage(const common::ErrorCode& error) { picojson::object obj; obj["error"] = picojson::value(static_cast(error)); return std::unique_ptr(new picojson::value(obj)); -- 2.7.4