Add handling exceptions to the client API 74/136874/23
authorPiotr Sawicki <p.sawicki2@partner.samsung.com>
Mon, 3 Jul 2017 07:51:57 +0000 (09:51 +0200)
committerPiotr Sawicki <p.sawicki2@partner.samsung.com>
Thu, 6 Jul 2017 18:01:11 +0000 (20:01 +0200)
Change-Id: I0e4c32b1329bdd266c0424534c86970c5a0f6ce1

src/client/api/askuser-notification-client.cpp
src/client/impl/TryCatch.h [new file with mode: 0644]

index 3d29b566624f4229129c111fc2fc67bea03ea6b5..c8e7602ed11e6da7854e7f6c52c878c00aedcbcc 100644 (file)
 
 #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;
@@ -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<AskUser::Client::ApiInterface> 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 (file)
index 0000000..84bdc83
--- /dev/null
@@ -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 <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
+
+