changeset 2234:a78d15509a1c

cleaner separation of PluginException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Dec 2016 14:35:35 +0100
parents 3dd44baebc36
children 99d151fafab9
files Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/Common/OrthancPluginException.h Plugins/Samples/ModalityWorklists/Plugin.cpp Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 5 files changed, 141 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Wed Dec 14 16:57:21 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Fri Dec 16 14:35:35 2016 +0100
@@ -38,32 +38,6 @@
 
 namespace OrthancPlugins
 {
-  const char* GetErrorDescription(OrthancPluginContext* context,
-                                  OrthancPluginErrorCode code)
-  {
-    const char* description = OrthancPluginGetErrorDescription(context, code);
-    if (description)
-    {
-      return description;
-    }
-    else
-    {
-      return "No description available";
-    }
-  }
-
-  
-#if HAS_ORTHANC_EXCEPTION == 0
-  void PluginException::Check(OrthancPluginErrorCode code)
-  {
-    if (code != OrthancPluginErrorCode_Success)
-    {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
-    }
-  }
-#endif
-
-
   void MemoryBuffer::Check(OrthancPluginErrorCode code)
   {
     if (code != OrthancPluginErrorCode_Success)
@@ -71,7 +45,7 @@
       // Prevent using garbage information
       buffer_.data = NULL;
       buffer_.size = 0;
-      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
     }
   }
 
@@ -125,7 +99,7 @@
     if (buffer_.data == NULL ||
         buffer_.size == 0)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
 
     const char* tmp = reinterpret_cast<const char*>(buffer_.data);
@@ -134,7 +108,7 @@
     if (!reader.parse(tmp, tmp + buffer_.size, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
   }
 
@@ -166,7 +140,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
     }
   }
 
@@ -200,7 +174,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
     }
   }
 
@@ -234,7 +208,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
     }
   }
 
@@ -287,7 +261,7 @@
   {
     if (str == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
     else
     {
@@ -325,14 +299,14 @@
     if (str_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
 
     Json::Reader reader;
     if (!reader.parse(str_, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
   }
 
@@ -358,7 +332,7 @@
     if (str.GetContent() == NULL)
     {
       OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
 
     str.ToJson(configuration_);
@@ -366,7 +340,7 @@
     if (configuration_.type() != Json::objectValue)
     {
       OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
   }
 
@@ -375,7 +349,7 @@
   {
     if (context_ == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_Plugin);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
     }
     else
     {
@@ -428,7 +402,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
       }
 
       target.configuration_ = configuration_[key];
@@ -454,7 +428,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
 
     target = configuration_[key].asString();
@@ -489,7 +463,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
   }
 
@@ -511,7 +485,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
     else
     {
@@ -539,7 +513,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
 
     target = configuration_[key].asBool();
@@ -578,7 +552,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
   }
 
@@ -673,7 +647,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Trying to access a NULL image");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -684,7 +658,7 @@
   {
     if (context == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -696,7 +670,7 @@
   {
     if (context == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
   
@@ -709,7 +683,7 @@
   {
     if (context == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
     else
     {
@@ -726,7 +700,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -739,7 +713,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -753,7 +727,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -845,7 +819,7 @@
   {
     if (worklist_ == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
 
@@ -860,7 +834,7 @@
     matcher_ = OrthancPluginCreateFindMatcher(context_, query, size);
     if (matcher_ == NULL)
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
   }
 
@@ -892,7 +866,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
 
     if (result == 0)
@@ -905,7 +879,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
     }
   }
 
@@ -1018,7 +992,7 @@
     }
     else
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
     }
   }
 
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Wed Dec 14 16:57:21 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Fri Dec 16 14:35:35 2016 +0100
@@ -32,23 +32,13 @@
 
 #pragma once
 
+#include "OrthancPluginException.h"
+
 #include <orthanc/OrthancCPlugin.h>
 #include <boost/noncopyable.hpp>
 #include <boost/lexical_cast.hpp>
 #include <json/value.h>
 
-#if !defined(HAS_ORTHANC_EXCEPTION)
-#  error The macro HAS_ORTHANC_EXCEPTION must be defined
-#endif
-
-
-#if HAS_ORTHANC_EXCEPTION == 1
-#  include "../../../Core/OrthancException.h"
-#  define ORTHANC_PLUGINS_THROW_EXCEPTION(code)  throw ::Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
-#else
-#  define ORTHANC_PLUGINS_THROW_EXCEPTION(code)  throw ::OrthancPlugins::PluginException(code)
-#endif
-
 
 #if (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER >= 2 ||   \
      (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == 1 &&  \
@@ -61,42 +51,12 @@
 
 
 
-
 namespace OrthancPlugins
 {
   typedef void (*RestCallback) (OrthancPluginRestOutput* output,
                                 const char* url,
                                 const OrthancPluginHttpRequest* request);
 
-  const char* GetErrorDescription(OrthancPluginContext* context,
-                                  OrthancPluginErrorCode code);
-
-#if HAS_ORTHANC_EXCEPTION == 0
-  class PluginException
-  {
-  private:
-    OrthancPluginErrorCode  code_;
-
-  public:
-    PluginException(OrthancPluginErrorCode code) : code_(code)
-    {
-    }
-
-    OrthancPluginErrorCode GetErrorCode() const
-    {
-      return code_;
-    }
-
-    const char* What(OrthancPluginContext* context) const
-    {
-      return ::OrthancPlugins::GetErrorDescription(context, code_);
-    }
-
-    static void Check(OrthancPluginErrorCode code);
-  };
-#endif
-
-
   class MemoryBuffer : public boost::noncopyable
   {
   private:
@@ -487,17 +447,10 @@
         Callback(output, url, request);
         return OrthancPluginErrorCode_Success;
       }
-#if HAS_ORTHANC_EXCEPTION == 1
-      catch (Orthanc::OrthancException& e)
+      catch (ORTHANC_PLUGINS_GET_EXCEPTION_CLASS& e)
       {
         return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
       }
-#else
-      catch (OrthancPlugins::PluginException& e)
-      {
-        return e.GetErrorCode();
-      }
-#endif
       catch (boost::bad_lexical_cast&)
       {
         return OrthancPluginErrorCode_BadFileFormat;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/Common/OrthancPluginException.h	Fri Dec 16 14:35:35 2016 +0100
@@ -0,0 +1,100 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#if !defined(HAS_ORTHANC_EXCEPTION)
+#  error The macro HAS_ORTHANC_EXCEPTION must be defined
+#endif
+
+
+#if HAS_ORTHANC_EXCEPTION == 1
+#  include "../../../Core/OrthancException.h"
+#  define ORTHANC_PLUGINS_GET_ERROR_ENUMERATION  ::Orthanc::ErrorCode
+#  define ORTHANC_PLUGINS_GET_ERROR_CODE(code)   ::Orthanc::ErrorCode_ ## code
+#  define ORTHANC_PLUGINS_GET_EXCEPTION_CLASS    ::Orthanc::OrthancException
+#else
+#  include <orthanc/OrthancCPlugin.h>
+#  define ORTHANC_PLUGINS_GET_ERROR_ENUMERATION  ::OrthancPluginErrorCode
+#  define ORTHANC_PLUGINS_GET_ERROR_CODE(code)   ::OrthancPluginErrorCode_ ## code
+#  define ORTHANC_PLUGINS_GET_EXCEPTION_CLASS    ::OrthancPlugins::PluginException
+#endif
+
+
+#define ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code)                   \
+  throw ORTHANC_PLUGINS_GET_EXCEPTION_CLASS(static_cast<ORTHANC_PLUGINS_GET_ERROR_ENUMERATION>(code));
+
+
+#define ORTHANC_PLUGINS_THROW_EXCEPTION(code)                           \
+  throw ORTHANC_PLUGINS_GET_EXCEPTION_CLASS(ORTHANC_PLUGINS_GET_ERROR_CODE(code));
+                                                  
+
+#define ORTHANC_PLUGINS_CHECK_ERROR(code)                           \
+  if (code != ORTHANC_PLUGINS_GET_ERROR_CODE(Success))              \
+  {                                                                 \
+    ORTHANC_PLUGINS_THROW_EXCEPTION(code);                          \
+  }
+
+
+namespace OrthancPlugins
+{
+#if HAS_ORTHANC_EXCEPTION == 0
+  class PluginException
+  {
+  private:
+    OrthancPluginErrorCode  code_;
+
+  public:
+    explicit PluginException(OrthancPluginErrorCode code) : code_(code)
+    {
+    }
+
+    OrthancPluginErrorCode GetErrorCode() const
+    {
+      return code_;
+    }
+
+    const char* What(OrthancPluginContext* context) const
+    {
+      const char* description = OrthancPluginGetErrorDescription(context, code_);
+      if (description)
+      {
+        return description;
+      }
+      else
+      {
+        return "No description available";
+      }
+    }
+  };
+#endif
+}
--- a/Plugins/Samples/ModalityWorklists/Plugin.cpp	Wed Dec 14 16:57:21 2016 +0100
+++ b/Plugins/Samples/ModalityWorklists/Plugin.cpp	Fri Dec 16 14:35:35 2016 +0100
@@ -51,7 +51,7 @@
     if (code != OrthancPluginErrorCode_Success)
     {
       OrthancPlugins::LogError(context_, "Error while adding an answer to a worklist request");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
+      ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
     }
   }
 }
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Wed Dec 14 16:57:21 2016 +0100
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Dec 16 14:35:35 2016 +0100
@@ -204,7 +204,7 @@
       }
       catch (...)
       {
-        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
       }
 
       boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path));
@@ -257,7 +257,7 @@
   if (folders.type() != Json::objectValue)
   {
     OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)");
-    ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+    ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
   }
 
   Json::Value::Members members = folders.getMemberNames();
@@ -270,7 +270,7 @@
     {
       OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + 
                                "\" must be associated with a string value (its mapped URI)");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
 
     std::string baseUri = *it;
@@ -291,7 +291,7 @@
     if (baseUri.empty())
     {
       OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
 
     // Check whether the source folder exists and is indeed a directory
@@ -299,7 +299,7 @@
     if (!boost::filesystem::is_directory(folder))
     {
       OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder);
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
     }
 
     folders_[baseUri] = folder;
@@ -318,7 +318,7 @@
   if (extensions.type() != Json::objectValue)
   {
     OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)");
-    ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+    ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
   }
 
   Json::Value::Members members = extensions.getMemberNames();
@@ -330,7 +330,7 @@
     {
       OrthancPlugins::LogError(context_, "The file extension \"" + *it + 
                                "\" must be associated with a string value (its MIME type)");
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
     }
 
     const std::string& mime = extensions[*it].asString();