Apply enum based exception handling
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 11 Nov 2019 06:05:33 +0000 (15:05 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 12 Nov 2019 05:19:57 +0000 (14:19 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/client/virtual-table.cpp
src/vist/client/virtual-table.hpp
src/vist/exception.hpp
src/vist/main/main.cpp
src/vist/policy/policy-loader.cpp
src/vist/policy/policy-loader.hpp
src/vist/policy/policy-manager.cpp
src/vist/policy/policy-manager.hpp
src/vist/policy/policy-storage.cpp
src/vist/service/vist.cpp

index 55783af9fe80d34784881848da8f35c76247d767..fe84e2d44d30cd272e18ea76c68fe5d8e7fca89d 100644 (file)
@@ -21,6 +21,7 @@
 #include <vist/client/schema/processes.hpp>
 #include <vist/client/schema/time.hpp>
 
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 #include <vist/query-builder.hpp>
 
@@ -76,11 +77,11 @@ template<typename Struct, typename Member>
 Member VirtualRow<T>::at(Member Struct::* field) const
 {
        if (this->data.size() == 0)
-               throw std::runtime_error("Data is not exist.");
+               THROW(ErrCode::RuntimeError) << "Data is not exist.";
 
        std::string key = metaDB.getColumnName(field);
        if (key.empty())
-               throw std::runtime_error("Key is not exist.");
+               THROW(ErrCode::RuntimeError) << "Column is not exist.";
 
        /// Convert "table.column" to "column"
        std::size_t pos = key.find(".");
@@ -89,14 +90,14 @@ Member VirtualRow<T>::at(Member Struct::* field) const
 
        std::string value = this->data.at(key);
        if (value.empty()) {
-               ERROR(VIST) << "The value of key[" << key << "] is not exist.";
+               ERROR(VIST) << "The value of column[" << key << "] is empty.";
                return Member();
        }
 
        try {
                return boost::lexical_cast<Member>(value);
        } catch (...) {
-               ERROR(VIST) << "Failed to casting [key]: " << key;
+               THROW(ErrCode::BadCast) << "Failed to casting [key]: " << key;
                return Member();
        }
 }
index e6da834cfbfeb0de30c90df2cc1feb3fdc7b8a4e..85de13129e881ab8a6766a14b88d7141cf3cd835 100644 (file)
@@ -17,7 +17,6 @@
 #pragma once
 
 #include <map>
-#include <stdexcept>
 #include <string>
 #include <vector>
 
@@ -43,7 +42,6 @@ private:
        KeyValuePair data;
 };
 
-
 template <typename T>
 class VirtualTable final {
 public:
index 5a8cec29e653b175b7061380f8832af46d41e748..9fa717e700fef69f158569eb9d0400c47d9f2582 100644 (file)
 
 namespace vist {
 
+enum class ErrCode {
+       LogicError = 0, /// Includes invalid_argument
+       RuntimeError,
+       BadCast
+};
+
 template <typename ErrCode>
 class Exception final : public std::exception {
 public:
index eec9680f7e8545b2f11f2a22bdef60573faebb72..8e64bbcc50a7c44af6e1682149a2daf141e0f8c0 100644 (file)
 
 #include <vist/service/vist.hpp>
 
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
+
 #include <cstdlib>
-#include <stdexcept>
 
 using namespace vist;
 
 int main() try {
        Vist::Instance().start();
        return EXIT_SUCCESS;
-} catch(const std::exception&) {
+} catch(const Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed while daemon is running." << e.what();
+       return EXIT_FAILURE;
+} catch(const std::exception& e) {
+       ERROR(VIST) << "Failed while daemon is running." << e.what();
        return EXIT_FAILURE;
 }
index 28c3b415e48a3bc1f021ea25dd320be3addef937..d8d5385c6353f3c30ab6df77cae3bec4eae62d02 100644 (file)
@@ -25,11 +25,13 @@ PolicyProvider* PolicyLoader::load(const std::string& path)
        PolicyProvider::FactoryType factory = nullptr;
        loader.load(PolicyProvider::getFactoryName(), factory);
        if (factory == nullptr)
-               std::runtime_error("Failed to load symbol. " + PolicyProvider::getFactoryName());
+               THROW(ErrCode::RuntimeError) << "Failed to load factory: "
+                                                                        << PolicyProvider::getFactoryName();
 
        auto provider = (*factory)();
        if (provider == nullptr)
-               std::runtime_error("Failed to make provider. " + PolicyProvider::getFactoryName());
+               THROW(ErrCode::RuntimeError) << "Failed to make provider: "
+                                                                        << PolicyProvider::getFactoryName();
 
        return provider;
 }
@@ -41,7 +43,7 @@ PluginLoader::PluginLoader(const std::string& path, int flag)
 //     : handle(::dlopen(path.c_str(), flag), ::dlclose)
 {
        if (handle == nullptr)
-               throw std::invalid_argument("Failed to open: " + path);
+               THROW(ErrCode::LogicError) << "Failed to open: " << path;
 }
 
 } // namespace policy
index 72212239fbd5a9557bf62717f6ea55dce0890fe1..fe830363e3287f39902d20236e11656dd3631a3d 100644 (file)
@@ -18,8 +18,9 @@
 
 #include <vist/sdk/policy-provider.hpp>
 
+#include <vist/exception.hpp>
+
 #include <memory>
-#include <stdexcept>
 #include <string>
 
 #include <dlfcn.h>
@@ -48,7 +49,7 @@ void PluginLoader::load(const std::string& name, T& symbol)
 {
        symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
        if (symbol == nullptr)
-               throw std::runtime_error("Failed to load: " + name);
+               THROW(ErrCode::RuntimeError) << "Failed to load: " << name;
 }
 
 } // namespace policy
index f1bd3e58200010803b499a6a1096f0f2fd27b646..4dee112eb8a9dcf9f3169735f9f90d5028e7a033 100644 (file)
@@ -17,6 +17,7 @@
 #include "policy-manager.hpp"
 #include "policy-loader.hpp"
 
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 
 #include <klay/filesystem.h>
@@ -36,7 +37,7 @@ std::pair<int, int> PolicyManager::loadProviders(const std::string& path)
        INFO(VIST) << "Load policies from :" << path;
        klay::File dir(path);
        if (!dir.exists() || !dir.isDirectory())
-               throw std::invalid_argument("Plugin directory is wrong.: " + path);
+               THROW(ErrCode::LogicError) << "Plugin directory is wrong.: " << path;
 
        int passed = 0, failed = 0;
        klay::DirectoryIterator end;
index 7523976351ed7ae1c37c945dab557701db016b89..103d616ad6ca0b858362900860545cb9c8f63bd7 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "policy-storage.hpp"
 
-#include <exception>
 #include <memory>
 #include <string>
 #include <unordered_map>
index 6cc3798eb80d3936742e7aeddf457896a9aa9612..3df7179384d93c1e0d0f774c8503b01773a9baaf 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "policy-storage.hpp"
 
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 #include <vist/query-builder.hpp>
 
@@ -120,12 +121,12 @@ std::string PolicyStorage::getScript(const std::string& name)
        std::string path = SCRIPT_BASE + "/" + name + ".sql";
        std::ifstream is(path);
        if (is.fail())
-               throw std::invalid_argument("Failed to open script: " + path)
+               THROW(ErrCode::LogicError) << "Failed to open script: " << path
 
        std::istreambuf_iterator<char> begin(is), end;
        auto content = std::string(begin, end);
        if (content.empty())
-               throw std::runtime_error("Failed to read script: " + path)
+               THROW(ErrCode::LogicError) << "Failed to read script: " << path
 
        return content;
 }
@@ -147,7 +148,7 @@ void PolicyStorage::define(const std::string& policy, int ivalue)
        stmt.bind(1, pd.name);
        stmt.bind(2, pd.ivalue);
        if (!stmt.exec())
-               throw std::runtime_error("Failed to define policy: " + pd.name);
+               THROW(ErrCode::RuntimeError) << "Failed to define policy: " << pd.name;
 }
 
 void PolicyStorage::enroll(const std::string& name)
@@ -163,7 +164,7 @@ void PolicyStorage::enroll(const std::string& name)
        database::Statement stmt(*database, query);
        stmt.bind(1, name);
        if (!stmt.exec())
-               throw std::runtime_error("Failed to enroll admin: " + name);
+               THROW(ErrCode::RuntimeError) << "Failed to enroll admin: " << name;
 
        admins.push_back(name);
        /// PolicyActivated is triggered by enrolling admin.
@@ -189,7 +190,7 @@ void PolicyStorage::disenroll(const std::string& name)
        database::Statement stmt(*database, query);
        stmt.bind(1, name);
        if (!stmt.exec())
-               throw std::runtime_error("Failed to disenroll admin: " + name);
+               THROW(ErrCode::RuntimeError) << "Failed to disenroll admin: " << name;
 }
 
 void PolicyStorage::update(const std::string& admin,
@@ -200,10 +201,10 @@ void PolicyStorage::update(const std::string& admin,
                                << ", about: " << policy << ", value: " << std::to_string(value);
 
        if (std::find(admins.begin(), admins.end(), admin) == admins.end())
-               throw std::runtime_error("Not exist admin: " + admin);
+               THROW(ErrCode::LogicError) << "Not exist admin: " << admin;
 
        if (definitions.find(policy) == definitions.end())
-               throw std::runtime_error("Not exist policy: " + policy);
+               THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
 
        int policyValue = value;
        std::string query = polActivatedTable.update(&PolicyActivated::value)
@@ -214,7 +215,7 @@ void PolicyStorage::update(const std::string& admin,
        stmt.bind(2, admin);
        stmt.bind(3, policy);
        if (!stmt.exec())
-               throw runtime::Exception("Failed to update policy:" + policy);
+               THROW(ErrCode::RuntimeError) << "Failed to update policy:" << policy;
 
        syncPolicyActivated();
 }
@@ -222,7 +223,7 @@ void PolicyStorage::update(const std::string& admin,
 PolicyValue PolicyStorage::strictest(const std::string& policy)
 {
        if (definitions.find(policy) == definitions.end())
-               throw std::runtime_error("Not exist policy: " + policy);
+               THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
 
        // There is no enrolled admins.
        if (activatedPolicies.size() == 0)
@@ -242,7 +243,7 @@ PolicyValue PolicyStorage::strictest(const std::string& policy)
        }
 
        if (strictestPtr == nullptr)
-               throw std::runtime_error("Not exist managed policy: " + policy);
+               THROW(ErrCode::RuntimeError) << "Not exist managed policy: " << policy;
 
        return std::move(*strictestPtr);
 }
index 76946c72513b0147d43f1c65dfbf7db948f61a75..860e227f84f7883f9f96c2c28d1ee480c56e38ac 100644 (file)
@@ -18,8 +18,7 @@
 
 #include <vist/ipc/server.hpp>
 #include <vist/logger.hpp>
-
-#include <stdexcept>
+#include <vist/exception.hpp>
 
 #include <osquery/registry_interface.h>
 #include <osquery/sql.h>
@@ -50,7 +49,7 @@ Rows Vist::query(const std::string& statement)
 {
        osquery::SQL sql(statement, true);
        if (!sql.ok())
-               throw std::runtime_error("Faild to execute query: " + sql.getMessageString());
+               THROW(ErrCode::RuntimeError) << "Faild to execute query: " << sql.getMessageString();
 
        return std::move(sql.rows());
 }