Add ConnectionException 81/138181/5
authorPiotr Sawicki <p.sawicki2@partner.samsung.com>
Tue, 11 Jul 2017 06:14:12 +0000 (08:14 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 12 Jul 2017 15:46:06 +0000 (15:46 +0000)
Change-Id: Ia523ba65284ad7612ecbaf165a0cfddf9a96d9a0

src/capi/test/privacy_privilege_manager_test.cpp
src/client/impl/TryCatch.h
src/ipc-lib/ask-user-client-channel.cpp
src/ipc-lib/ask-user-server-channel.cpp
src/ipc-lib/askuser-notification/connection-exception.h [new file with mode: 0644]

index a596f38ef45b7491e64fcbb7a774b50a301acdf1..f20629dac5ca315100fae97f49b9ea0bf9bb25f7 100644 (file)
@@ -26,6 +26,7 @@
 #include <set>
 #include <stdio.h>
 #include <string.h>
+#include <sys/types.h>
 #include <glib.h>
 #include <unistd.h>
 
@@ -33,6 +34,7 @@
 #include <askuser-notification/ask-user-client-channel.h>
 #include <askuser-notification/ask-user-server-channel.h>
 #include <askuser-notification/ask-user-types.h>
+#include <askuser-notification/connection-exception.h>
 #include <privacy_privilege_manager.h>
 
 using namespace AskUser::Protocol;
@@ -564,11 +566,9 @@ int main(int argc, char **argv)
         switch (c) {
             case 's':
                 ctx.m_mode = Mode::SERVER;
-                print_server_help();
                 break;
             case 'c':
                 ctx.m_mode = Mode::CLIENT;
-                print_client_help();
                 break;
             case 'h':
             default:
@@ -584,11 +584,33 @@ int main(int argc, char **argv)
     try {
         ctx.initialize();
     }
+    catch (const ConnectionException &e) {
+        printf("Connection exception occured during initialization: %s, exiting.\n", e.what());
+        printf("Check if the /var/run/user_ext/%u directory exists.\n", geteuid());
+        exit(EXIT_FAILURE);
+    }
+    catch (const std::exception &e) {
+        printf("Standard exception occured during initialization: %s, exiting.\n", e.what());
+        exit(EXIT_FAILURE);
+    }
     catch (...) {
-        printf("Unknwon exception occured, exiting.\n");
+        printf("Unknwon exception occured during initialization, exiting.\n");
         exit(EXIT_FAILURE);
     }
 
+    switch (ctx.m_mode) {
+        case Mode::SERVER:
+            print_server_help();
+            break;
+        case Mode::CLIENT:
+            print_client_help();
+            break;
+        case Mode::UNKNOWN:
+        default:
+            printf("Unknown mode\n");
+            exit(EXIT_FAILURE);
+    }
+
     GMainLoop *mainloop = g_main_loop_new(nullptr, FALSE);
 
     GIOChannel *gio_channel = g_io_channel_unix_new(0);
index 84bdc83d81f95cc297432c0206a72717f218f0cc..52d84cc82568f9885dfab82a93a71771b6a47a3c 100644 (file)
@@ -27,6 +27,7 @@
 #include <functional>
 
 #include <log/alog.h>
+#include <askuser-notification/connection-exception.h>
 
 #include <askuser-notification-client.h>
 
@@ -37,6 +38,9 @@ namespace Client {
 int tryCatch(const std::function<int(void)> &func) {
     try {
         return func();
+    } catch (const Protocol::ConnectionException &e) {
+        ALOGE(e.what());
+        return ASKUSER_API_CONNECTION_ERROR;
     } catch (const std::bad_alloc &e) {
         ALOGE(e.what());
         return ASKUSER_API_OUT_OF_MEMORY;
index 7a55d52b049ffa321c106fdfb0242550a6076d32..035d503b1edc33f105805c7bae1dcf03ab284327 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <attributes/attributes.h>
 #include <askuser-notification/ask-user-client-channel.h>
+#include <askuser-notification/connection-exception.h>
 
 #include <ask-user-config.h>
 #include <message-utils.h>
@@ -44,7 +45,7 @@ ClientChannel::~ClientChannel() {
 void ClientChannel::init() {
     Sock s(Sock::CLI_STREAM);
     if (0 > s.connect(getStreamSocketPath(geteuid())))
-        throw std::logic_error("Cannot connect to the server");
+        throw ConnectionException("Cannot connect to the server");
 
     int fd = s.getFd();
     m_sockets[fd] = SockDesc(std::move(s));
index 215a1f9306f5cf45bd0bfcdedb52a42357ff3049..d6578f316e927e66b3bc756c7898bcd84eecf4f7 100644 (file)
@@ -23,6 +23,7 @@
 #include <sstream>
 
 #include <askuser-notification/ask-user-server-channel.h>
+#include <askuser-notification/connection-exception.h>
 
 #include <ask-user-config.h>
 #include <message-utils.h>
@@ -44,7 +45,8 @@ ServerChannel::~ServerChannel() {
 
 void ServerChannel::init() {
     Sock stream(Sock::SRV_STREAM);
-    stream.connect(getStreamSocketPath(geteuid()));
+    if (0 > stream.connect(getStreamSocketPath(geteuid())))
+        throw ConnectionException("Cannot create server socket");
 
     int fd = stream.getFd();
     m_sockets[fd] = SockDesc(std::move(stream));
diff --git a/src/ipc-lib/askuser-notification/connection-exception.h b/src/ipc-lib/askuser-notification/connection-exception.h
new file mode 100644 (file)
index 0000000..c3baeee
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co.
+ *
+ *  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        connection-exception.h
+ * @author      Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @brief       The declaration of ConnectionException.
+ */
+
+#pragma once
+
+#include <exception>
+#include <string>
+
+namespace AskUser {
+
+namespace Protocol {
+
+class ConnectionException : public std::exception
+{
+public:
+    ConnectionException(const std::string &msg) : m_msg(msg) {
+    }
+
+    virtual const char* what() const noexcept {
+        return m_msg.c_str();
+    }
+
+private:
+    std::string m_msg;
+};
+
+} // namespace Protocol
+
+} // namespace AskUser
+