From c2893b506684cc51013d79325074deb5cb6934de Mon Sep 17 00:00:00 2001 From: Ossama Othman Date: Mon, 16 Dec 2013 12:49:42 -0800 Subject: [PATCH] Do not terminate settingsd on plugin exception. Improve exception reporting, as well. Change-Id: I6e4c839caca4a08c71e15335d2997403f39aa21b Signed-off-by: Ossama Othman --- lib/loader.cpp | 6 +++--- lib/manager.cpp | 22 +++++++++++++--------- src/websocket_server.cpp | 6 ++++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/loader.cpp b/lib/loader.cpp index b017879..3df7eb2 100644 --- a/lib/loader.cpp +++ b/lib/loader.cpp @@ -31,7 +31,7 @@ #include #include -#include +#include #include @@ -49,7 +49,7 @@ ivi::settings::loader::loader(std::string const & plugin_name, : handle_(dlopen(plugin_name.c_str(), RTLD_LAZY | RTLD_GLOBAL)) { if (handle_ == nullptr) { - throw std::logic_error(dlerror()); + throw std::system_error(std::error_code(), dlerror()); } // Reset any lingering dynamic linking related errors (see @@ -64,7 +64,7 @@ ivi::settings::loader::loader(std::string const & plugin_name, "register_settings")); if (register_settings == nullptr) - throw std::logic_error(dlerror()); + throw std::system_error(std::error_code(), dlerror()); // Now create the underlying settings implementation. diff --git a/lib/manager.cpp b/lib/manager.cpp index 3f8ad7c..1766d94 100644 --- a/lib/manager.cpp +++ b/lib/manager.cpp @@ -69,8 +69,8 @@ void ivi::settings::manager::load_settings(std::string const & dir) { if (dir.length() == 0) - throw std::runtime_error("Zero length settings plugin " - "directory path."); + throw std::invalid_argument("Zero length settings plugin " + "directory path."); namespace fs = boost::filesystem; @@ -103,7 +103,7 @@ ivi::settings::manager::load_settings(std::string const & dir) loaders_.push_back(std::move(plugin)); } - catch (std::logic_error & e) { + catch (std::exception & e) { /** * @todo I really hate catching an exception like this, but I * really don't want to resort to a construct/init() @@ -111,19 +111,22 @@ ivi::settings::manager::load_settings(std::string const & dir) */ std::cerr << "Error loading plugin: " << e.what() << "\n"; } + catch (...) { + std::cerr << "Ignoring unknown exception thrown from plugin.\n"; + } } if (settings_.size() == 0) { - throw std::runtime_error("Settings plugin directory \"" - + dir + "\" has no plugins."); + throw std::invalid_argument("Settings plugin directory \"" + + dir + "\" has no plugins."); } else { - std::cerr << "Registered settings plugins: \n"; + std::cerr << "Registered settings plugins:\n"; for (auto const & i : settings_) std::cerr << "\t" << i.first << '\n'; } } else { - throw std::runtime_error("Settings plugin directory \"" - + dir + "\" does not exist."); + throw std::invalid_argument("Settings plugin directory \"" + + dir + "\" does not exist."); } } @@ -140,7 +143,8 @@ ivi::settings::manager::dispatch(std::string request, unique_ptr const parser(json_parser_new()); json_parser_load_from_data(parser.get(), request.c_str(), -1, nullptr); - JsonReader * const reader = json_reader_new(json_parser_get_root(parser.get())); + JsonReader * const reader = + json_reader_new(json_parser_get_root(parser.get())); unique_ptr const safe_reader(reader); // Retrieve setting name and transcation ID from the JSON request diff --git a/src/websocket_server.cpp b/src/websocket_server.cpp index 636e893..5f158af 100644 --- a/src/websocket_server.cpp +++ b/src/websocket_server.cpp @@ -29,7 +29,7 @@ #include "../lib/config.hpp" #include "../lib/manager.hpp" -#include +#include @@ -136,7 +136,9 @@ ivi::settings::websocket_server::websocket_server( context_ = libwebsocket_create_context(&info); if (context_ == nullptr) - throw std::runtime_error("Unable to initialize websocket."); + throw std::system_error(errno, + std::system_category(), + "Unable to initialize websocket."); std::cout << "\n" PACKAGE_NAME " listening for requests on port " << info.port << ".\n"; -- 2.7.4