From a0f0fc8723493bf273af39547bc55fedf9ea6de5 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 25 Oct 2016 07:07:22 +0200 Subject: [PATCH] refactor: Add module::halt(error) --- include/modules/i3.hpp | 16 +++++++++++----- include/modules/meta.hpp | 38 +++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/include/modules/i3.hpp b/include/modules/i3.hpp index 38e5ea66..303cd827 100644 --- a/include/modules/i3.hpp +++ b/include/modules/i3.hpp @@ -87,18 +87,24 @@ namespace modules { try { m_ipc.subscribe(i3ipc::ET_WORKSPACE); m_ipc.prepare_to_event_handling(); - } catch (std::runtime_error& e) { - throw module_error(e.what()); + } catch (std::runtime_error& err) { + throw module_error(err.what()); + } catch (std::exception& err) { + throw module_error(err.what()); } // }}} } void stop() { - // Shutdown ipc connection when before stopping the module {{{ + // Shutdown ipc connection when stopping the module {{{ + + try { + shutdown(m_ipc.get_event_socket_fd(), SHUT_RD); + shutdown(m_ipc.get_main_socket_fd(), SHUT_RD); + } catch (...) { + } - shutdown(m_ipc.get_event_socket_fd(), SHUT_RD); - shutdown(m_ipc.get_main_socket_fd(), SHUT_RD); event_module::stop(); // }}} diff --git a/include/modules/meta.hpp b/include/modules/meta.hpp index eede79c3..02e86af4 100644 --- a/include/modules/meta.hpp +++ b/include/modules/meta.hpp @@ -152,6 +152,7 @@ namespace modules { virtual void setup() = 0; virtual void start() = 0; virtual void stop() = 0; + virtual void halt(string error_message) = 0; virtual string contents() = 0; virtual bool handle_event(string cmd) = 0; @@ -214,7 +215,16 @@ namespace modules { void setup() { m_log.trace("%s: Setup", name()); - CAST_MOD(Impl)->setup(); + + try { + CAST_MOD(Impl)->setup(); + } catch (const module_error& err) { + m_log.err("%s: Setup failed", name()); + CAST_MOD(Impl)->halt(err.what()); + } catch (const std::exception& err) { + m_log.err("%s: Setup failed", name()); + CAST_MOD(Impl)->halt(err.what()); + } } void stop() { @@ -232,6 +242,12 @@ namespace modules { m_terminator(name()); } + void halt(string error_message) { + m_log.err("%s: %s", name(), error_message); + m_log.warn("Stopping '%s'...", name()); + stop(); + } + void teardown() { CAST_MOD(Impl)->wakeup(); } @@ -353,8 +369,8 @@ namespace modules { using module::module; void start() { - CAST_MOD(Impl)->setup(); CAST_MOD(Impl)->enable(true); + CAST_MOD(Impl)->setup(); CAST_MOD(Impl)->broadcast(); } @@ -393,10 +409,10 @@ namespace modules { } CAST_MOD(Impl)->sleep(m_interval); } + } catch (const module_error& err) { + CAST_MOD(Impl)->halt(err.what()); } catch (const std::exception& err) { - this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); - this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name()); - CAST_MOD(Impl)->stop(); + CAST_MOD(Impl)->halt(err.what()); } } }; @@ -433,10 +449,10 @@ namespace modules { else CAST_MOD(Impl)->broadcast(); } + } catch (const module_error& err) { + CAST_MOD(Impl)->halt(err.what()); } catch (const std::exception& err) { - this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); - this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name()); - CAST_MOD(Impl)->stop(); + CAST_MOD(Impl)->halt(err.what()); } } }; @@ -464,10 +480,10 @@ namespace modules { while (CAST_MOD(Impl)->enabled()) { CAST_MOD(Impl)->poll_events(); } + } catch (const module_error& err) { + CAST_MOD(Impl)->halt(err.what()); } catch (const std::exception& err) { - this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); - this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name()); - CAST_MOD(Impl)->stop(); + CAST_MOD(Impl)->halt(err.what()); } }