From: Piotr Sawicki Date: Mon, 3 Jul 2017 07:51:57 +0000 (+0200) Subject: Add handling exceptions to the client API X-Git-Tag: submit/tizen/20170727.154157~1^2~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=34ff2ff4f1be6796d1fc3f7e7cb39a13045bd4e3;p=platform%2Fcore%2Fsecurity%2Faskuser.git Add handling exceptions to the client API Change-Id: I0e4c32b1329bdd266c0424534c86970c5a0f6ce1 --- diff --git a/src/client/api/askuser-notification-client.cpp b/src/client/api/askuser-notification-client.cpp index 3d29b56..c8e7602 100644 --- a/src/client/api/askuser-notification-client.cpp +++ b/src/client/api/askuser-notification-client.cpp @@ -22,13 +22,15 @@ #include +#include +#include +#include + #include #include #include #include - -#include -#include +#include struct askuser_client { AskUser::Client::ApiInterface *impl; @@ -45,10 +47,13 @@ API int askuser_client_initialize(askuser_client **pp_client, askuser_status_callback status_callback, void *p_user_data) { - if (!pp_client) + if (!pp_client) { return ASKUSER_API_INVALID_PARAM; + } - try { + init_agent_log(); + + return AskUser::Client::tryCatch([&]() { AskUser::Client::StatusCallbackClosure closure(status_callback, p_user_data); std::unique_ptr ptr; @@ -57,56 +62,48 @@ int askuser_client_initialize(askuser_client **pp_client, *pp_client = new askuser_client(ptr.get()); ptr.release(); - } catch (...) { - // TODO handle exceptions - return ASKUSER_API_UNKNOWN_ERROR; - } - return ASKUSER_API_SUCCESS; + return ASKUSER_API_SUCCESS; + }); } API void askuser_client_finalize(askuser_client *p_client) { - if (!p_client) + if (!p_client) { return; + } - try { + AskUser::Client::tryCatch([&]() { delete p_client; - } catch (...) { - // TODO - } + return ASKUSER_API_SUCCESS; + }); } API int askuser_client_process(askuser_client *p_client, int fd, int events) { - if (!p_client) + if (!p_client) { return ASKUSER_API_INVALID_PARAM; + } - try { + return AskUser::Client::tryCatch([&]() { return p_client->impl->process(fd, events); - } catch (...) { - // TODO - return ASKUSER_API_UNKNOWN_ERROR; - } + }); } API int askuser_client_check_privilege(askuser_client *p_client, const char *privilege, askuser_check_result *p_result) { - if (!p_client || !privilege || !p_result) + if (!p_client || !privilege || !p_result) { return ASKUSER_API_INVALID_PARAM; - - try { - *p_result = p_client->impl->checkPrivilege(privilege); - } catch(...) { - // TODO handle exceptions - return ASKUSER_API_UNKNOWN_ERROR; } - return ASKUSER_API_SUCCESS; + return AskUser::Client::tryCatch([&]() { + *p_result = p_client->impl->checkPrivilege(privilege); + return ASKUSER_API_SUCCESS; + }); } API @@ -114,20 +111,18 @@ int askuser_client_popup_request(askuser_client *p_client, const char *privilege askuser_popup_response_callback response_callback, void *p_user_data, int *p_request_id) { - if (!p_client || !response_callback || !privilege) + if (!p_client || !privilege || !response_callback) { return ASKUSER_API_INVALID_PARAM; + } - try { + return AskUser::Client::tryCatch([&]() { AskUser::Client::PopupCallbackClosure closure(response_callback, p_user_data); AskUser::Client::RequestId id = p_client->impl->popupRequest(closure, privilege); - if (p_request_id != nullptr) { + if (p_request_id) { *p_request_id = id; } - } catch (...) { - // TODO handle exceptions - return ASKUSER_API_UNKNOWN_ERROR; - } - return ASKUSER_API_SUCCESS; + return ASKUSER_API_SUCCESS; + }); } diff --git a/src/client/impl/TryCatch.h b/src/client/impl/TryCatch.h new file mode 100644 index 0000000..84bdc83 --- /dev/null +++ b/src/client/impl/TryCatch.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file TryCatch.h + * @author Piotr Sawicki + * @version 1.0 + * @brief The definition of tryCatch helper. + */ + +#pragma once + +#include +#include + +#include + +#include + +namespace AskUser { + +namespace Client { + +int tryCatch(const std::function &func) { + try { + return func(); + } catch (const std::bad_alloc &e) { + ALOGE(e.what()); + return ASKUSER_API_OUT_OF_MEMORY; + } catch (const std::exception &e) { + ALOGE(e.what()); + return ASKUSER_API_UNKNOWN_ERROR; + } catch (...) { + ALOGE("Unknown exception"); + return ASKUSER_API_UNKNOWN_ERROR; + } +} + +} // namespace Client + +} // namespace AskUser + +