#include <memory>
+#include <log/alog.h>
+#include <askuser-notification-client.h>
+#include <attributes/attributes.h>
+
#include <ApiInterface.h>
#include <ApiInterfaceImpl.h>
#include <PopupCallbackClosure.h>
#include <StatusCallbackClosure.h>
-
-#include <askuser-notification-client.h>
-#include <attributes/attributes.h>
+#include <TryCatch.h>
struct askuser_client {
AskUser::Client::ApiInterface *impl;
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<AskUser::Client::ApiInterface> ptr;
*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
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;
+ });
}
--- /dev/null
+/*
+ * 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 <p.sawicki2@partner.samsung.com>
+ * @version 1.0
+ * @brief The definition of tryCatch helper.
+ */
+
+#pragma once
+
+#include <exception>
+#include <functional>
+
+#include <log/alog.h>
+
+#include <askuser-notification-client.h>
+
+namespace AskUser {
+
+namespace Client {
+
+int tryCatch(const std::function<int(void)> &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
+
+