changeset 1651:2e692c83e2f3

improved custom error login
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Sep 2015 17:29:39 +0200
parents 9f34ebfaf2c9
children 0c86b30bb8b2
files Core/Enumerations.cpp OrthancServer/main.cpp Plugins/Engine/OrthancPluginDatabase.cpp Plugins/Engine/OrthancPluginDatabase.h Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/PluginsErrorDictionary.cpp Plugins/Engine/PluginsErrorDictionary.h Plugins/Samples/Basic/Plugin.c
diffstat 8 files changed, 104 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.cpp	Fri Sep 25 16:47:05 2015 +0200
+++ b/Core/Enumerations.cpp	Fri Sep 25 17:29:39 2015 +0200
@@ -44,6 +44,11 @@
   // "Resources/GenerateErrorCodes.py"
   const char* EnumerationToString(ErrorCode error)
   {
+    if (error >= ErrorCode_START_PLUGINS)
+    {
+      return "Error encountered within some plugin";
+    }
+
     switch (error)
     {
       case ErrorCode_InternalError:
--- a/OrthancServer/main.cpp	Fri Sep 25 16:47:05 2015 +0200
+++ b/OrthancServer/main.cpp	Fri Sep 25 17:29:39 2015 +0200
@@ -333,7 +333,7 @@
 #if ORTHANC_PLUGINS_ENABLED == 1
       if (plugins_ != NULL)
       {
-        plugins_->GetErrorDictionary().LogError(exception);
+        plugins_->GetErrorDictionary().LogError(exception.GetErrorCode(), true);
         isPlugin = true;
       }
 #endif
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Sep 25 17:29:39 2015 +0200
@@ -194,11 +194,13 @@
 
 
   OrthancPluginDatabase::OrthancPluginDatabase(SharedLibrary& library,
+                                               PluginsErrorDictionary&  errorDictionary,
                                                const OrthancPluginDatabaseBackend& backend,
                                                const OrthancPluginDatabaseExtensions* extensions,
                                                size_t extensionsSize,
                                                void *payload) : 
     library_(library),
+    errorDictionary_(errorDictionary),
     type_(_OrthancPluginDatabaseAnswerType_None),
     backend_(backend),
     payload_(payload),
@@ -236,6 +238,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -248,6 +251,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -259,6 +263,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -270,6 +275,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -284,6 +290,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -298,6 +305,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -310,6 +318,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -321,6 +330,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -357,6 +367,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -379,6 +390,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
 
@@ -432,6 +444,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -446,6 +459,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -462,6 +476,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -483,6 +498,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -500,6 +516,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -517,6 +534,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -532,6 +550,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -546,6 +565,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
     
@@ -566,6 +586,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -581,6 +602,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -596,6 +618,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -611,6 +634,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -626,6 +650,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -641,6 +666,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -657,6 +683,7 @@
  
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -688,6 +715,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -724,6 +752,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -746,6 +775,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -762,6 +792,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -792,6 +823,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -814,6 +846,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -830,6 +863,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -847,6 +881,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -863,6 +898,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -880,6 +916,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -909,6 +946,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -925,6 +963,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
 
@@ -940,6 +979,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -967,6 +1007,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -981,6 +1022,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -993,6 +1035,7 @@
 
     if (error != OrthancPluginErrorCode_Success)
     {
+      errorDictionary_.LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -1003,12 +1046,15 @@
   private:
     const OrthancPluginDatabaseBackend& backend_;
     void* payload_;
+    PluginsErrorDictionary&  errorDictionary_;
 
   public:
     Transaction(const OrthancPluginDatabaseBackend& backend,
-                void* payload) :
+                void* payload,
+                PluginsErrorDictionary&  errorDictionary) :
       backend_(backend),
-      payload_(payload)
+      payload_(payload),
+      errorDictionary_(errorDictionary)
     {
     }
 
@@ -1018,6 +1064,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
@@ -1028,6 +1075,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
@@ -1038,6 +1086,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
@@ -1046,7 +1095,7 @@
 
   SQLite::ITransaction* OrthancPluginDatabase::StartTransaction()
   {
-    return new Transaction(backend_, payload_);
+    return new Transaction(backend_, payload_, errorDictionary_);
   }
 
 
@@ -1093,6 +1142,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
 
@@ -1119,6 +1169,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        errorDictionary_.LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
--- a/Plugins/Engine/OrthancPluginDatabase.h	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Engine/OrthancPluginDatabase.h	Fri Sep 25 17:29:39 2015 +0200
@@ -36,6 +36,7 @@
 
 #include "../../OrthancServer/IDatabaseWrapper.h"
 #include "../Include/orthanc/OrthancCDatabasePlugin.h"
+#include "PluginsErrorDictionary.h"
 #include "SharedLibrary.h"
 
 namespace Orthanc
@@ -48,6 +49,7 @@
     typedef std::pair<int64_t, ResourceType>  AnswerResource;
 
     SharedLibrary&  library_;
+    PluginsErrorDictionary&  errorDictionary_;
     _OrthancPluginDatabaseAnswerType type_;
     OrthancPluginDatabaseBackend backend_;
     OrthancPluginDatabaseExtensions extensions_;
@@ -82,6 +84,7 @@
 
   public:
     OrthancPluginDatabase(SharedLibrary& library,
+                          PluginsErrorDictionary&  errorDictionary,
                           const OrthancPluginDatabaseBackend& backend,
                           const OrthancPluginDatabaseExtensions* extensions,
                           size_t extensionsSize,
--- a/Plugins/Engine/OrthancPlugins.cpp	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Sep 25 17:29:39 2015 +0200
@@ -66,6 +66,7 @@
     {
     private:
       _OrthancPluginRegisterStorageArea callbacks_;
+      PluginsErrorDictionary&  errorDictionary_;
 
       void Free(void* buffer) const
       {
@@ -76,7 +77,10 @@
       }
 
     public:
-      PluginStorageArea(const _OrthancPluginRegisterStorageArea& callbacks) : callbacks_(callbacks)
+      PluginStorageArea(const _OrthancPluginRegisterStorageArea& callbacks,
+                        PluginsErrorDictionary&  errorDictionary) : 
+        callbacks_(callbacks),
+        errorDictionary_(errorDictionary)
       {
       }
 
@@ -91,6 +95,7 @@
 
         if (error != OrthancPluginErrorCode_Success)
         {
+          errorDictionary_.LogError(error, true);
           throw OrthancException(static_cast<ErrorCode>(error));
         }
       }
@@ -108,6 +113,7 @@
 
         if (error != OrthancPluginErrorCode_Success)
         {
+          errorDictionary_.LogError(error, true);
           throw OrthancException(static_cast<ErrorCode>(error));
         }
 
@@ -138,6 +144,7 @@
 
         if (error != OrthancPluginErrorCode_Success)
         {
+          errorDictionary_.LogError(error, true);
           throw OrthancException(static_cast<ErrorCode>(error));
         }
       }
@@ -149,12 +156,15 @@
     private:
       SharedLibrary&   sharedLibrary_;
       _OrthancPluginRegisterStorageArea  callbacks_;
+      PluginsErrorDictionary&  errorDictionary_;
 
     public:
       StorageAreaFactory(SharedLibrary& sharedLibrary,
-                         const _OrthancPluginRegisterStorageArea& callbacks) :
+                         const _OrthancPluginRegisterStorageArea& callbacks,
+                         PluginsErrorDictionary&  errorDictionary) :
         sharedLibrary_(sharedLibrary),
-        callbacks_(callbacks)
+        callbacks_(callbacks),
+        errorDictionary_(errorDictionary)
       {
       }
 
@@ -165,7 +175,7 @@
 
       IStorageArea* Create() const
       {
-        return new PluginStorageArea(callbacks_);
+        return new PluginStorageArea(callbacks_, errorDictionary_);
       }
     };
   }
@@ -464,6 +474,7 @@
     }
     else
     {
+      GetErrorDictionary().LogError(error, true);
       throw OrthancException(static_cast<ErrorCode>(error));
     }
   }
@@ -485,6 +496,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        GetErrorDictionary().LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
@@ -507,6 +519,7 @@
 
       if (error != OrthancPluginErrorCode_Success)
       {
+        GetErrorDictionary().LogError(error, true);
         throw OrthancException(static_cast<ErrorCode>(error));
       }
     }
@@ -1378,7 +1391,7 @@
         
         if (pimpl_->storageArea_.get() == NULL)
         {
-          pimpl_->storageArea_.reset(new StorageAreaFactory(plugin, p));
+          pimpl_->storageArea_.reset(new StorageAreaFactory(plugin, p, GetErrorDictionary()));
         }
         else
         {
@@ -1457,7 +1470,8 @@
 
         if (pimpl_->database_.get() == NULL)
         {
-          pimpl_->database_.reset(new OrthancPluginDatabase(plugin, *p.backend, NULL, 0, p.payload));
+          pimpl_->database_.reset(new OrthancPluginDatabase(plugin, GetErrorDictionary(), 
+                                                            *p.backend, NULL, 0, p.payload));
         }
         else
         {
@@ -1478,7 +1492,8 @@
 
         if (pimpl_->database_.get() == NULL)
         {
-          pimpl_->database_.reset(new OrthancPluginDatabase(plugin, *p.backend, p.extensions,
+          pimpl_->database_.reset(new OrthancPluginDatabase(plugin, GetErrorDictionary(),
+                                                            *p.backend, p.extensions,
                                                             p.extensionsSize, p.payload));
         }
         else
--- a/Plugins/Engine/PluginsErrorDictionary.cpp	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Engine/PluginsErrorDictionary.cpp	Fri Sep 25 17:29:39 2015 +0200
@@ -88,12 +88,13 @@
   }
 
 
-  void  PluginsErrorDictionary::LogError(const OrthancException& exception)
+  void  PluginsErrorDictionary::LogError(ErrorCode code,
+                                         bool ignoreBuiltinErrors)
   {
-    if (exception.GetErrorCode() >= ErrorCode_START_PLUGINS)
+    if (code >= ErrorCode_START_PLUGINS)
     {
       boost::mutex::scoped_lock lock(mutex_);
-      Errors::const_iterator error = errors_.find(static_cast<int32_t>(exception.GetErrorCode()));
+      Errors::const_iterator error = errors_.find(static_cast<int32_t>(code));
       
       if (error != errors_.end())
       {
@@ -104,7 +105,11 @@
       }
     }
 
-    LOG(ERROR) << "Exception inside the plugin engine: " << exception.What();
+    if (!ignoreBuiltinErrors)
+    {
+      LOG(ERROR) << "Exception inside the plugin engine: "
+                 << EnumerationToString(code);
+    }
   }
 
 
--- a/Plugins/Engine/PluginsErrorDictionary.h	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Engine/PluginsErrorDictionary.h	Fri Sep 25 17:29:39 2015 +0200
@@ -74,7 +74,14 @@
                                      uint16_t httpStatus,
                                      const char* message);
 
-    void  LogError(const OrthancException& exception);
+    void  LogError(ErrorCode code,
+                   bool ignoreBuiltinErrors);
+
+    void  LogError(OrthancPluginErrorCode code,
+                   bool ignoreBuiltinErrors)
+    {
+      LogError(static_cast<ErrorCode>(code), ignoreBuiltinErrors);
+    }
 
     bool  Format(Json::Value& message,    /* out */
                  HttpStatus& httpStatus,  /* out */
--- a/Plugins/Samples/Basic/Plugin.c	Fri Sep 25 16:47:05 2015 +0200
+++ b/Plugins/Samples/Basic/Plugin.c	Fri Sep 25 17:29:39 2015 +0200
@@ -25,7 +25,7 @@
 
 static OrthancPluginContext* context = NULL;
 
-static OrthancPluginErrorCode c1;
+static OrthancPluginErrorCode customError;
 
 
 ORTHANC_PLUGINS_API int32_t Callback1(OrthancPluginRestOutput* output,
@@ -216,8 +216,6 @@
                                                                const char* url,
                                                                const OrthancPluginHttpRequest* request)
 {
-  return c1;
-      
   const char* pathLocator = "\"Path\" : \"";
   char info[1024];
   char *id, *eos;
@@ -405,7 +403,7 @@
   sprintf(info, "[ \"STORESCP\", \"localhost\", 2000 ]");
   OrthancPluginRestApiPut(context, &tmp, "/modalities/demo", info, strlen(info));
 
-  c1 = OrthancPluginRegisterErrorCode(context, 4, 402, "Hello world");
+  customError = OrthancPluginRegisterErrorCode(context, 4, 402, "Hello world");
 
   return 0;
 }