Fix to use vist exception
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 28 Nov 2019 07:55:08 +0000 (16:55 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 2 Dec 2019 06:36:38 +0000 (15:36 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/event/eventfd.cpp
src/vist/event/mainloop.cpp
src/vist/klass/functor.hpp
src/vist/rmi/server.cpp
src/vist/sdk/policy-model.hpp
src/vist/transport/socket.cpp
src/vist/transport/socket.hpp

index 3e276928f211256be88e6bc1b60a2c9eb47974dc..a211bb39e8bbc1776fc466de157c12959c04516d 100644 (file)
 
 #include "eventfd.hpp"
 
+#include <vist/exception.hpp>
+
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <cstdint>
-#include <stdexcept>
 
 namespace vist {
 namespace event {
@@ -35,7 +36,7 @@ EventFD::EventFD(unsigned int initval, int flags)
        : fd(::eventfd(initval, flags))
 {
        if (this->fd == -1)
-               throw std::runtime_error("Failed to create eventfd.");
+               THROW(ErrCode::RuntimeError) << "Failed to create eventfd.";
 }
 
 EventFD::~EventFD()
@@ -47,14 +48,14 @@ void EventFD::send(void)
 {
        const std::uint64_t val = 1;
        if (::write(this->fd, &val, sizeof(val)) == -1)
-               throw std::runtime_error("Failed to write to eventfd.");
+               THROW(ErrCode::RuntimeError) << "Failed to write to eventfd.";
 }
 
 void EventFD::receive(void)
 {
        std::uint64_t val;
        if (::read(this->fd, &val, sizeof(val)) == -1)
-               throw std::runtime_error("Failed to read from eventfd.");
+               THROW(ErrCode::RuntimeError) << "Failed to read from eventfd.";
 }
 
 int EventFD::getFd(void) const noexcept
index 991ceb87a2957b186b8d81e82bfebfd1c3925d4e..4e721045cb50f26991db31ef91c92c3691d5cb34 100644 (file)
@@ -22,6 +22,7 @@
 #include "mainloop.hpp"
 
 #include <vist/logger.hpp>
+#include <vist/exception.hpp>
 
 #include <errno.h>
 #include <unistd.h>
@@ -36,7 +37,7 @@ Mainloop::Mainloop() :
        stopped(false)
 {
        if (epollFd == -1)
-               throw std::runtime_error("Failed to create epoll instance.");
+               THROW(ErrCode::RuntimeError) << "Failed to create epoll instance.";
 }
 
 Mainloop::~Mainloop()
@@ -49,7 +50,7 @@ void Mainloop::addHandler(const int fd, OnEvent&& onEvent, OnError&& onError)
        std::lock_guard<Mutex> lock(mutex);
 
        if (this->listener.find(fd) != this->listener.end())
-               throw std::runtime_error("Event is already registered.");
+               THROW(ErrCode::RuntimeError) << "Event is already registered.";
 
        ::epoll_event event;
        std::memset(&event, 0, sizeof(epoll_event));
@@ -58,7 +59,7 @@ void Mainloop::addHandler(const int fd, OnEvent&& onEvent, OnError&& onError)
        event.data.fd = fd;
 
        if (::epoll_ctl(this->epollFd, EPOLL_CTL_ADD, fd, &event) == -1)
-               throw std::runtime_error("Failed to add event source.");
+               THROW(ErrCode::RuntimeError) << "Failed to add event source.";
 
        auto onEventPtr = std::make_shared<OnEvent>(onEvent);
        auto onErrorPtr = (onError != nullptr) ? std::make_shared<OnError>(onError) : nullptr;
index c9872fc4bd6883a6cfcda76f2903c20e4d08a159..63cd9dbcee4448bfb9d610943546dd549fc705b0 100644 (file)
 #pragma once
 
 #include <vist/archive.hpp>
+#include <vist/exception.hpp>
 #include <vist/klass/function.hpp>
 
 #include <functional>
 #include <memory>
-#include <stdexcept>
 #include <unordered_map>
 
 namespace vist {
@@ -139,7 +139,7 @@ template<typename R, typename K, typename... Ps>
 Functor<R, K, Ps...> make_functor(std::shared_ptr<K> instance, R (K::* member)(Ps...))
 {
        if (instance == nullptr)
-               throw std::invalid_argument("Instance can't be nullptr.");
+               THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
 
        return Functor<R, K, Ps...>(instance, make_function(member));
 }
@@ -149,7 +149,7 @@ std::shared_ptr<Functor<R, K, Ps...>> make_functor_ptr(K* instance,
                                                                                                           R (K::* member)(Ps...))
 {
        if (instance == nullptr)
-               throw std::invalid_argument("Instance can't be nullptr.");
+               THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
 
        std::shared_ptr<K> smartPtr(instance);
        return std::make_shared<Functor<R, K, Ps...>>(smartPtr, make_function(member));
@@ -160,7 +160,7 @@ std::shared_ptr<Functor<R, K, Ps...>> make_functor_ptr(std::shared_ptr<K> instan
                                                                                                           R (K::* member)(Ps...))
 {
        if (instance == nullptr)
-               throw std::invalid_argument("Instance can't be nullptr.");
+               THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
 
        return std::make_shared<Functor<R, K, Ps...>>(instance, make_function(member));
 }
index 269fa9c4df95178e354cfa495323bb440fbb2cb8..02062600bed10d5670182bb762854439e12ac4d8 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "server.hpp"
 
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 #include <vist/transport/message.hpp>
 
@@ -62,7 +63,7 @@ void Server::listen(const std::string& socketPath)
 void Server::onAccept(std::shared_ptr<Connection>&& connection)
 {
        if (connection == nullptr)
-               throw std::invalid_argument("Wrong connection.");
+               THROW(ErrCode::LogicError) << "Wrong connection.";
 
        auto onRead = [this, connection]() {
                std::shared_ptr<Connection> conn;
@@ -71,7 +72,7 @@ void Server::onAccept(std::shared_ptr<Connection>&& connection)
 
                auto iter = this->connectionMap.find(connection->getFd());
                if (iter == this->connectionMap.end())
-                       throw std::runtime_error("Faild to find connection.");
+                       THROW(ErrCode::RuntimeError) << "Faild to find connection.";
 
                conn = iter->second;
 
@@ -98,14 +99,14 @@ void Server::onAccept(std::shared_ptr<Connection>&& connection)
 void Server::onClose(const std::shared_ptr<Connection>& connection)
 {
        if (connection == nullptr)
-               throw std::invalid_argument("Wrong connection.");
+               THROW(ErrCode::LogicError) << "Wrong connection.";
 
        {
                std::lock_guard<std::mutex> lock(this->connectionMutex);
 
                auto iter = this->connectionMap.find(connection->getFd());
                if (iter == this->connectionMap.end())
-                       throw std::runtime_error("Faild to find connection.");
+                       THROW(ErrCode::RuntimeError) << "Faild to find connection.";
 
                this->mainloop.removeHandler(iter->first);
                INFO(VIST) << "Connection is closed. fd: " << iter->first;
@@ -123,7 +124,7 @@ void Server::dispatch(const std::shared_ptr<Connection>& connection)
 
                auto iter = this->functorMap.find(funcName);
                if (iter == this->functorMap.end())
-                       throw std::runtime_error("Faild to find function.");
+                       THROW(ErrCode::RuntimeError) << "Faild to find function.";
 
                DEBUG(VIST) << "Remote method invokation: " << funcName;
 
index 20a52292f1ff626530434b37e2f1b2ed659ff7da..7ecd69e5aa447df159a666bb7f1d17c07c7f290c 100644 (file)
 
 #pragma once
 
+#include <vist/exception.hpp>
 #include <vist/sdk/policy-value.hpp>
 
-#include <string>
 #include <stdexcept>
+#include <string>
 
 namespace vist {
 namespace policy {
@@ -51,7 +52,7 @@ public:
        inline const PolicyValue& get() const
        {
                if (!ready)
-                       throw std::runtime_error("Policy value should be set once before use.");
+                       THROW(ErrCode::RuntimeError) << "Policy value should be set once before use.";
 
                return current;
        }
index cb7bbc4687e1b7926f56259cfabf8f21b436e5d5..4040232c6cb060e7dfd5a6e7a19dfe3d5a883e5f 100644 (file)
@@ -38,7 +38,7 @@ namespace {
 void set_cloexec(int fd)
 {
        if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
-               throw std::runtime_error("Failed to set CLOSEXEC.");
+               THROW(ErrCode::RuntimeError) << "Failed to set CLOSEXEC.";
 }
 
 } // anonymous namespace
@@ -50,11 +50,11 @@ Socket::Socket(int fd) noexcept : fd(fd)
 Socket::Socket(const std::string& path)
 {
        if (path.size() >= sizeof(::sockaddr_un::sun_path))
-               throw std::invalid_argument("Socket path size is wrong.");
+               THROW(ErrCode::LogicError) << "Socket path size is wrong.";
 
        int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd == -1)
-               throw std::runtime_error("Failed to create socket.");
+               THROW(ErrCode::RuntimeError) << "Failed to create socket.";
 
        set_cloexec(fd);
 
@@ -68,16 +68,16 @@ Socket::Socket(const std::string& path)
        struct stat buf;
        if (::stat(path.c_str(), &buf) == 0)
                if (::unlink(path.c_str()) == -1)
-                       throw std::runtime_error("Failed to remove exist socket.");
+                       THROW(ErrCode::RuntimeError) << "Failed to remove exist socket.";
 
        if (::bind(fd, reinterpret_cast<::sockaddr*>(&addr), sizeof(::sockaddr_un)) == -1) {
                ::close(fd);
-               throw std::runtime_error("Failed to bind.");
+               THROW(ErrCode::RuntimeError) << "Failed to bind.";
        }
 
        if (::listen(fd, MAX_BACKLOG_SIZE) == -1) {
                ::close(fd);
-               throw std::runtime_error("Failed to liten.");
+               THROW(ErrCode::RuntimeError) << "Failed to listen.";
        }
 
        this->fd = fd;
@@ -109,7 +109,7 @@ Socket Socket::accept(void) const
 {
        int fd = ::accept(this->fd, nullptr, nullptr);
        if (fd == -1)
-               throw std::runtime_error("Failed to accept.");
+               THROW(ErrCode::RuntimeError) << "Failed to accept.";
 
        set_cloexec(fd);
 
@@ -119,11 +119,11 @@ Socket Socket::accept(void) const
 Socket Socket::connect(const std::string& path)
 {
        if (path.size() >= sizeof(::sockaddr_un::sun_path))
-               throw std::invalid_argument("Socket path size is wrong.");
+               THROW(ErrCode::LogicError) << "Socket path size is wrong.";
 
        int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd == -1)
-               throw std::runtime_error("Failed to create socket.");
+               THROW(ErrCode::RuntimeError) << "Failed to create socket.";
 
        set_cloexec(fd);
 
@@ -136,7 +136,7 @@ Socket Socket::connect(const std::string& path)
 
        if (::connect(fd, reinterpret_cast<::sockaddr*>(&addr), sizeof(sockaddr_un)) == -1) {
                ::close(fd);
-               throw std::runtime_error("Failed to connect.");
+               THROW(ErrCode::RuntimeError) << "Failed to connect.";
        }
 
        return Socket(fd);
index 69069f46b1772486ee2996c11919d2f1b8a3176b..0f0d53b77cee8e55f945fa3b4817bc3a0d8e773d 100644 (file)
 
 #pragma once
 
+#include <vist/exception.hpp>
+
 #include <errno.h>
 #include <unistd.h>
 
 #include <cstddef>
-#include <stdexcept>
 #include <string>
 
 namespace vist {
@@ -73,7 +74,7 @@ void Socket::send(const T *buffer, const std::size_t size) const
                else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
                        continue;
                else
-                       std::runtime_error("Failed to write.");
+                       THROW(ErrCode::RuntimeError) << "Failed to write to socket.";
        }
 }