Add enum based exception handling
authorSangwan Kwon <sangwan.kwon@samsung.com>
Wed, 6 Nov 2019 05:36:43 +0000 (14:36 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Fri, 8 Nov 2019 05:40:37 +0000 (14:40 +0900)
Standard c++ throw can not provide information like function, file,
line number. It makes hard to tracking where the exception occured.
So, add macro(THROW) which can track informations.

Syntax:
- THROW(ErrCode::LogicError) << "Additional information about exception";

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/CMakeLists.txt
src/vist/common/CMakeLists.txt [new file with mode: 0644]
src/vist/common/exception.h [new file with mode: 0644]
src/vist/common/tests/exception.cpp [new file with mode: 0644]

index 8f71945..73f9be1 100644 (file)
@@ -29,6 +29,7 @@ ADD_DEFINITIONS(-DDB_PATH="${DB_INSTALL_DIR}/.vist.db"
                                -DSCRIPT_INSTALL_DIR="${SCRIPT_INSTALL_DIR}")
 
 ADD_SUBDIRECTORY(client)
+ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(notification)
 ADD_SUBDIRECTORY(policy)
 ADD_SUBDIRECTORY(service)
diff --git a/src/vist/common/CMakeLists.txt b/src/vist/common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..04eeb46
--- /dev/null
@@ -0,0 +1,16 @@
+#  Copyright (c) 2019 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(GLOB COMMON_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${COMMON_TESTS})
diff --git a/src/vist/common/exception.h b/src/vist/common/exception.h
new file mode 100644 (file)
index 0000000..8236026
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ *  Copyright (c) 2019 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   exception.h
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @brief  Enum based exception handling
+ * @usage
+ *  enum class ErrCode {
+ *    LogicError = 1,
+ *    RuntimeError
+ *  };
+ *
+ *  try {
+ *    THROW(ErrCode::LogicError) << "Additional information about exception";
+ *  } catch (const vist::Exception<ErrCode>& e) {
+ *    ErrCode ec = e.get();
+ *    std::string message = e.what();
+ *  }
+ */
+
+#pragma once
+
+#include <cstring>
+#include <exception>
+#include <sstream>
+#include <string>
+#include <type_traits>
+#include <typeinfo>
+
+#include <boost/core/demangle.hpp>
+
+#ifndef __FILENAME__
+#define __FILENAME__ \
+       ::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__
+#endif
+
+#define THROW(ec) \
+       throw vist::Exception<decltype(ec)>(ec,__FILENAME__,__FUNCTION__,__LINE__)
+
+namespace vist {
+
+template <typename ErrCode>
+class Exception final : public std::exception {
+public:
+       static_assert(std::is_enum<ErrCode>::value, "Error code must be enum type.");
+
+       using Self = Exception<ErrCode>;
+
+       Exception(ErrCode ec, const char *file,
+                         const char *function, unsigned int line) noexcept : ec(ec)
+       {
+               std::stringstream ss;
+               ss << "[" << file << ":" << line << " " << function << "()]"
+                  << "[" << boost::core::demangle(typeid(ec).name())
+                  << "(" << static_cast<typename std::underlying_type_t<ErrCode>>(ec) << ")]";
+               message = ss.str();
+       }
+
+       virtual const char* what() const noexcept override
+       {
+               return message.c_str();
+       }
+
+       inline ErrCode get() const noexcept
+       {
+               return ec;
+       }
+
+       template<typename T>
+       Self& operator<<(const T& arg) noexcept
+       {
+               message += (static_cast<std::stringstream&>(std::stringstream() << arg)).str();
+               return *this;
+       }
+
+private:
+       ErrCode ec;
+       std::string message;
+};
+
+} // namespace vist
diff --git a/src/vist/common/tests/exception.cpp b/src/vist/common/tests/exception.cpp
new file mode 100644 (file)
index 0000000..9dd4f5e
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include <gtest/gtest.h>
+
+#include "../exception.h"
+
+enum class ErrCode {
+       InvalidArgument = 1,
+       DominError,
+       LogicError,
+       RuntimeError,
+       None
+};
+
+class ExceptionTests : public testing::Test {};
+
+TEST_F(ExceptionTests, exception)
+{
+       bool raised = false;
+       ErrCode ec = ErrCode::None;
+       std::string msg;
+
+       try {
+               THROW(ErrCode::InvalidArgument);
+       } catch (const vist::Exception<ErrCode>& e) {
+               raised = true;
+               ec = e.get();
+               msg = e.what();
+       }
+
+       EXPECT_TRUE(raised);
+       EXPECT_EQ(ec, ErrCode::InvalidArgument);
+       EXPECT_NE(std::string::npos, msg.find("ErrCode"));
+}
+
+TEST_F(ExceptionTests, exception_msg)
+{
+       bool raised = false;
+       ErrCode ec = ErrCode::None;
+       std::string msg;
+
+       try {
+               THROW(ErrCode::RuntimeError) << "Additional error message";
+       } catch (const vist::Exception<ErrCode>& e) {
+               raised = true;
+               ec = e.get();
+               msg = e.what();
+       }
+
+       EXPECT_TRUE(!raised) << msg;
+       EXPECT_EQ(ec, ErrCode::RuntimeError);
+       EXPECT_NE(std::string::npos, msg.find("Additional"));
+}