changeset 587:312af09354e0

Irccd: add more information in plugin_error, closes #746 While here, improve the error reporting when loading plugins.
author David Demelier <markand@malikania.fr>
date Tue, 05 Dec 2017 21:42:55 +0100
parents f462cc16b517
children be1656362297
files libirccd-js/irccd/js/js_plugin.cpp libirccd/irccd/plugin.cpp libirccd/irccd/plugin.hpp libirccd/irccd/plugin_service.cpp libirccd/irccd/plugin_service.hpp
diffstat 5 files changed, 73 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-js/irccd/js/js_plugin.cpp	Tue Dec 05 21:09:45 2017 +0100
+++ b/libirccd-js/irccd/js/js_plugin.cpp	Tue Dec 05 21:42:55 2017 +0100
@@ -125,7 +125,7 @@
 
     // TODO: add error message here.
     if (!input)
-        throw plugin_error(plugin_error::exec_error);
+        throw plugin_error(plugin_error::exec_error, name(), std::strerror(errno));
 
     std::string data(
         std::istreambuf_iterator<char>(input.rdbuf()),
@@ -133,7 +133,7 @@
     );
 
     if (duk_peval_string(context_, data.c_str()))
-        throw plugin_error(plugin_error::exec_error);
+        throw plugin_error(plugin_error::exec_error, name(), dukx_stack(context_, -1).stack());
 
     // Read metadata.
     duk_get_global_string(context_, "info");
--- a/libirccd/irccd/plugin.cpp	Tue Dec 05 21:09:45 2017 +0100
+++ b/libirccd/irccd/plugin.cpp	Tue Dec 05 21:42:55 2017 +0100
@@ -18,6 +18,8 @@
 
 #include <boost/filesystem.hpp>
 
+#include <sstream>
+
 #include "plugin.hpp"
 #include "system.hpp"
 
@@ -49,7 +51,25 @@
             return plugin;
     }
 
-    throw plugin_error(plugin_error::not_found);
+    throw plugin_error(plugin_error::not_found, name);
+}
+
+plugin_error::plugin_error(error errc, std::string name, std::string message) noexcept
+    : system_error(make_error_code(errc))
+    , name_(std::move(name))
+    , message_(std::move(message))
+{
+    std::ostringstream oss;
+
+    oss << "plugin " << name_ << ": " << code().message();
+
+    std::istringstream iss(message_);
+    std::string line;
+
+    while (getline(iss, line))
+        oss << "\nplugin " << name_ << ": " << line;
+
+    what_ = oss.str();
 }
 
 const boost::system::error_category& plugin_category()
--- a/libirccd/irccd/plugin.hpp	Tue Dec 05 21:09:45 2017 +0100
+++ b/libirccd/irccd/plugin.hpp	Tue Dec 05 21:42:55 2017 +0100
@@ -545,10 +545,48 @@
         already_exists,
     };
 
+private:
+    std::string name_;
+    std::string message_;
+    std::string what_;
+
+public:
     /**
-     * Inherited constructors.
+     * Constructor.
+     *
+     * \param code the error code
+     * \param name the plugin name
+     * \param message the optional message (e.g. error from plugin)
+     */
+    plugin_error(error code, std::string name, std::string message = "") noexcept;
+
+    /**
+     * Get the plugin name.
+     *
+     * \return the name
      */
-    using system_error::system_error;
+    inline const std::string& name() const noexcept
+    {
+        return name_;
+    }
+
+    /**
+     * Get the additional message.
+     *
+     * \return the message
+     */
+    inline const std::string& message() const noexcept
+    {
+        return message_;
+    }
+
+    /**
+     * Get message appropriate for use with logger.
+     */
+    const char* what() const noexcept override
+    {
+        return what_.c_str();
+    }
 };
 
 /**
--- a/libirccd/irccd/plugin_service.cpp	Tue Dec 05 21:09:45 2017 +0100
+++ b/libirccd/irccd/plugin_service.cpp	Tue Dec 05 21:42:55 2017 +0100
@@ -82,7 +82,7 @@
     auto plugin = get(name);
 
     if (!plugin)
-        throw plugin_error(plugin_error::not_found);
+        throw plugin_error(plugin_error::not_found, name);
 
     return plugin;
 }
@@ -159,7 +159,7 @@
 void plugin_service::load(std::string name, std::string path)
 {
     if (has(name))
-        throw plugin_error(plugin_error::already_exists);
+        throw plugin_error(plugin_error::already_exists, name);
 
     std::shared_ptr<plugin> plugin;
 
@@ -169,7 +169,7 @@
         plugin = open(name, std::move(path));
 
     if (!plugin)
-        throw plugin_error(plugin_error::not_found);
+        throw plugin_error(plugin_error::not_found, name);
 
     plugin->set_config(config(name));
     plugin->set_formats(formats(name));
@@ -184,7 +184,7 @@
     auto plugin = get(name);
 
     if (!plugin)
-        throw plugin_error(plugin_error::not_found);
+        throw plugin_error(plugin_error::not_found, name);
 
     exec(plugin, &plugin::on_reload, irccd_);
 }
@@ -196,7 +196,7 @@
     });
 
     if (it == plugins_.end())
-        throw plugin_error(plugin_error::not_found);
+        throw plugin_error(plugin_error::not_found, name);
 
     // Erase first, in case of throwing.
     auto save = *it;
@@ -222,8 +222,8 @@
         } else {
             try {
                 load(name, option.value());
-            } catch (const plugin_error& ex) {
-                log::warning() << "plugin " << option.key() << ": " << ex.what() << std::endl;
+            } catch (const std::exception& ex) {
+                log::warning(ex.what());
             }
         }
     }
--- a/libirccd/irccd/plugin_service.hpp	Tue Dec 05 21:09:45 2017 +0100
+++ b/libirccd/irccd/plugin_service.hpp	Tue Dec 05 21:42:55 2017 +0100
@@ -200,9 +200,9 @@
         try {
             ((*plugin).*(fn))(std::forward<Args>(args)...);
         } catch (const std::exception& ex) {
-            throw plugin_error(plugin_error::exec_error, ex.what());
+            throw plugin_error(plugin_error::exec_error, plugin->name(), ex.what());
         } catch (...) {
-            throw plugin_error(plugin_error::exec_error);
+            throw plugin_error(plugin_error::exec_error, plugin->name());
         }
     }
 
@@ -219,7 +219,7 @@
         auto plugin = find(name);
 
         if (!plugin)
-            throw plugin_error(plugin_error::not_found);
+            throw plugin_error(plugin_error::not_found, plugin->name());
 
         exec(plugin, fn, std::forward<Args>(args)...);
     }