Let tryCatch be used in multiple translation units 46/234346/7
authorZofia Abramowska <z.abramowska@samsung.com>
Mon, 25 May 2020 16:45:47 +0000 (18:45 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Thu, 9 Jul 2020 14:08:29 +0000 (16:08 +0200)
Move tryCatch() implementation to separate source file.

Change-Id: Ia4614a032a022765c386144e76c58c2eec6ab6b6

src/common/CMakeLists.txt
src/common/exceptions/TryCatch.cpp [new file with mode: 0644]
src/common/exceptions/TryCatch.h
src/helpers/creds-commons/CMakeLists.txt
src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
src/helpers/session/session.cpp
test/CMakeLists.txt

index 6ee49121f29bac8ce99d9efa29ec987569075d6d..51371b1b8af53e3bfbd2824288375c6b0515f027 100644 (file)
@@ -30,6 +30,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/containers/BinaryQueue.cpp
     ${COMMON_PATH}/error/api.cpp
     ${COMMON_PATH}/error/SafeStrError.cpp
+    ${COMMON_PATH}/exceptions/TryCatch.cpp
     ${COMMON_PATH}/lock/FileLock.cpp
     ${COMMON_PATH}/log/AuditLog.cpp
     ${COMMON_PATH}/log/log.cpp
diff --git a/src/common/exceptions/TryCatch.cpp b/src/common/exceptions/TryCatch.cpp
new file mode 100644 (file)
index 0000000..4192f9c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ *  Copyright (c) 2020 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        src/common/exceptions/TryCatch.cpp
+ * @author      Marcin Niesluchowski <m.niesluchow@samsung.com>
+ * @author      Oskar Świtalski <o.switalski@samsung.com>
+ * @version     1.0
+ * @brief       This file contains an implementation of a function for catching exceptions
+ */
+
+#include <cxxabi.h>
+#include <exception>
+#include <new>
+
+#include <exceptions/AccessDeniedException.h>
+#include <exceptions/InvalidProtocolException.h>
+#include <exceptions/NoMemoryException.h>
+#include <log/log.h>
+
+#include <cynara-error.h>
+#include "TryCatch.h"
+
+namespace Cynara {
+
+int tryCatch(const std::function<int(void)> &func) {
+    try {
+        return func();
+    } catch (const std::bad_alloc &e) {
+        LOGE_NOTHROW("%s", e.what());
+        return CYNARA_API_OUT_OF_MEMORY;
+    } catch (const NoMemoryException &e) {
+        LOGE_NOTHROW("%s", e.what());
+        return CYNARA_API_OUT_OF_MEMORY;
+    } catch (const InvalidProtocolException &e) {
+        LOGE_NOTHROW("%s", e.what());
+        return CYNARA_API_INVALID_PARAM;
+    } catch (const AccessDeniedException &e) {
+        LOGE_NOTHROW("%s", e.what());
+        return CYNARA_API_PERMISSION_DENIED;
+    } catch (const std::exception &e) {
+        LOGE_NOTHROW("%s", e.what());
+        return CYNARA_API_UNKNOWN_ERROR;
+    } catch (const abi::__forced_unwind &) {
+        /**
+         * workaround for pthread_cancel, which cancels thread using this exception
+         * and expects it to be rethrown in every try-catch
+         */
+        LOGD_NOTHROW("We are 'gracefully' stopped by pthread_cancel");
+        throw;
+    } catch (...) {
+        LOGE_NOTHROW("Unexpected exception");
+        return CYNARA_API_UNKNOWN_ERROR;
+    }
+}
+
+} // namespace Cynara
+
index d7f8aef22df8cab2c3212f1f8b47fbcdf64a2c5c..0b3b3c2d5cf79a4b86a1792c3c88dd56dfd85142 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2020 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.
  * @author      Marcin Niesluchowski <m.niesluchow@samsung.com>
  * @author      Oskar Świtalski <o.switalski@samsung.com>
  * @version     1.0
- * @brief       This file contains functions for catching exceptions
+ * @brief       This file contains a declaration of a function for catching exceptions
  */
 
 #ifndef SRC_COMMON_EXCEPTIONS_TRYCATCH_H_
 #define SRC_COMMON_EXCEPTIONS_TRYCATCH_H_
 
-#include <cxxabi.h>
-#include <exception>
 #include <functional>
-#include <new>
-
-#include <exceptions/AccessDeniedException.h>
-#include <exceptions/InvalidProtocolException.h>
-#include <exceptions/NoMemoryException.h>
-#include <log/log.h>
-
-#include <cynara-error.h>
 
 namespace Cynara {
 
-int tryCatch(const std::function<int(void)> &func) {
-    try {
-        return func();
-    } catch (const std::bad_alloc &e) {
-        LOGE_NOTHROW("%s", e.what());
-        return CYNARA_API_OUT_OF_MEMORY;
-    } catch (const NoMemoryException &e) {
-        LOGE_NOTHROW("%s", e.what());
-        return CYNARA_API_OUT_OF_MEMORY;
-    } catch (const InvalidProtocolException &e) {
-        LOGE_NOTHROW("%s", e.what());
-        return CYNARA_API_INVALID_PARAM;
-    } catch (const AccessDeniedException &e) {
-        LOGE_NOTHROW("%s", e.what());
-        return CYNARA_API_PERMISSION_DENIED;
-    } catch (const std::exception &e) {
-        LOGE_NOTHROW("%s", e.what());
-        return CYNARA_API_UNKNOWN_ERROR;
-    } catch (const abi::__forced_unwind &) {
-        /**
-         * workaround for pthread_cancel, which cancels thread using this exception
-         * and expects it to be rethrown in every try-catch
-         */
-        LOGD_NOTHROW("We are 'gracefully' stopped by pthread_cancel");
-        throw;
-    } catch (...) {
-        LOGE_NOTHROW("Unexpected exception");
-        return CYNARA_API_UNKNOWN_ERROR;
-    }
-}
+int tryCatch(const std::function<int(void)> &func);
 
 } // namespace Cynara
 
index db8064f7e09f43e69cb9b2952acb4ff05dd027c8..34fbe243650aeacbc0336d789b9eab771364c611 100644 (file)
@@ -24,6 +24,7 @@ SET(LIB_CREDS_COMMONS_VERSION ${LIB_CREDS_COMMONS_VERSION_MAJOR}.14.26)
 SET(LIB_CREDS_COMMONS_PATH ${CYNARA_PATH}/helpers/creds-commons)
 
 SET(LIB_CREDS_COMMONS_SOURCES
+    ${CYNARA_PATH}/common/exceptions/TryCatch.cpp
     ${LIB_CREDS_COMMONS_PATH}/creds-commons.cpp
     ${LIB_CREDS_COMMONS_PATH}/CredsCommonsInner.cpp
     )
index e15a564b28bd221029ef7738f8ff00ce5fc87655..b13e3f4426361ab1d954b8c2ef7ed3e25b6f18df 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <cynara-error.h>
 #include <exceptions/TryCatch.h>
+#include <log/log.h>
 
 #include <error/SafeStrError.h>
 
index 01f735570c4fe6f4bae50e46e87099cea8ac4601..024b5879e604b0d134834c87efdacc4780d8d300 100644 (file)
@@ -29,6 +29,7 @@
 #include <attributes/attributes.h>
 #include <exceptions/TryCatch.h>
 
+#include <cynara-error.h>
 #include <cynara-session.h>
 
 CYNARA_API
index 25c62252baa0aaed7b482bbfdfb065c54474a7d7..6e200a9571a390206e4894f479b1458189013366 100644 (file)
@@ -50,6 +50,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/common/config/PathConfig.cpp
     ${CYNARA_SRC}/common/containers/BinaryQueue.cpp
     ${CYNARA_SRC}/common/error/SafeStrError.cpp
+    ${CYNARA_SRC}/common/exceptions/TryCatch.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolAdmin.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolFrame.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolFrameHeader.cpp