changeset 1644:939b921b2c81

plugin error dictionary
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Sep 2015 22:05:27 +0200
parents 87c77b9b3679
children 1558b3226b18
files CMakeLists.txt Core/Enumerations.h Core/HttpServer/MongooseServer.cpp Core/OrthancException.h Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Engine/PluginsErrorDictionary.cpp Plugins/Engine/PluginsErrorDictionary.h Plugins/Engine/PluginsManager.cpp Plugins/Engine/PluginsManager.h Resources/GenerateErrorCodes.py
diffstat 11 files changed, 251 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Sep 23 14:42:20 2015 +0200
+++ b/CMakeLists.txt	Wed Sep 23 22:05:27 2015 +0200
@@ -222,6 +222,7 @@
     Plugins/Engine/OrthancPluginDatabase.cpp
     Plugins/Engine/OrthancPlugins.cpp
     Plugins/Engine/PluginsEnumerations.cpp
+    Plugins/Engine/PluginsErrorDictionary.cpp
     Plugins/Engine/PluginsManager.cpp
     Plugins/Engine/SharedLibrary.cpp
     )
--- a/Core/Enumerations.h	Wed Sep 23 14:42:20 2015 +0200
+++ b/Core/Enumerations.h	Wed Sep 23 22:05:27 2015 +0200
@@ -133,7 +133,8 @@
     ErrorCode_LuaReturnsNoString = 2035    /*!< The Lua function does not return a string */,
     ErrorCode_StorageAreaAlreadyRegistered = 2036    /*!< Another plugin has already registered a custom storage area */,
     ErrorCode_DatabaseBackendAlreadyRegistered = 2037    /*!< Another plugin has already registered a custom database back-end */,
-    ErrorCode_DatabaseNotInitialized = 2038    /*!< Plugin trying to call the database during its initialization */
+    ErrorCode_DatabaseNotInitialized = 2038    /*!< Plugin trying to call the database during its initialization */,
+    ErrorCode_START_PLUGINS = 1000000
   };
 
   enum LogLevel
--- a/Core/HttpServer/MongooseServer.cpp	Wed Sep 23 14:42:20 2015 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Wed Sep 23 22:05:27 2015 +0200
@@ -754,13 +754,15 @@
       LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
 
       Json::Value message = Json::objectValue;
+      message["Method"] = EnumerationToString(method);
+      message["Uri"] = request->uri;
+
+      // TODO
       message["HttpError"] = EnumerationToString(e.GetHttpStatus());
       message["HttpStatus"] = e.GetHttpStatus();
       message["Message"] = e.What();
-      message["Method"] = EnumerationToString(method);
       message["OrthancError"] = EnumerationToString(e.GetErrorCode());
       message["OrthancStatus"] = e.GetErrorCode();
-      message["Uri"] = request->uri;
 
       std::string info = message.toStyledString();
 
--- a/Core/OrthancException.h	Wed Sep 23 14:42:20 2015 +0200
+++ b/Core/OrthancException.h	Wed Sep 23 22:05:27 2015 +0200
@@ -43,39 +43,21 @@
   protected:
     ErrorCode  errorCode_;
     HttpStatus httpStatus_;
-    int32_t    pluginCode_;
-
-    OrthancException(ErrorCode errorCode,
-                     HttpStatus httpStatus,
-                     int32_t pluginCode) :
-      errorCode_(errorCode),
-      httpStatus_(httpStatus),
-      pluginCode_(0)
-    {
-    }
 
   public:
     OrthancException(ErrorCode errorCode) : 
       errorCode_(errorCode),
-      httpStatus_(ConvertErrorCodeToHttpStatus(errorCode)),
-      pluginCode_(0)
+      httpStatus_(ConvertErrorCodeToHttpStatus(errorCode))
     {
     }
 
     OrthancException(ErrorCode errorCode,
                      HttpStatus httpStatus) :
       errorCode_(errorCode),
-      httpStatus_(httpStatus),
-      pluginCode_(0)
+      httpStatus_(httpStatus)
     {
     }
 
-    static OrthancException GetPluginException(int32_t  pluginCode,
-                                               HttpStatus  httpStatus)
-    {
-      return OrthancException(ErrorCode_Plugin, httpStatus, pluginCode);
-    }
-
     ErrorCode GetErrorCode() const
     {
       return errorCode_;
@@ -86,11 +68,6 @@
       return httpStatus_;
     }
 
-    int32_t GetPluginErrorCode() const
-    {
-      return pluginCode_;
-    }
-
     const char* What() const
     {
       return EnumerationToString(errorCode_);
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 23 14:42:20 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 23 22:05:27 2015 +0200
@@ -242,6 +242,7 @@
     int argc_;
     char** argv_;
     std::auto_ptr<OrthancPluginDatabase>  database_;
+    PluginsErrorDictionary  dictionary_;
 
     PImpl() : 
       context_(NULL), 
@@ -1784,4 +1785,10 @@
   {
     return pimpl_->manager_;
   }
+
+
+  PluginsErrorDictionary&  OrthancPlugins::GetErrorDictionary()
+  {
+    return pimpl_->dictionary_;
+  }
 }
--- a/Plugins/Engine/OrthancPlugins.h	Wed Sep 23 14:42:20 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.h	Wed Sep 23 22:05:27 2015 +0200
@@ -32,6 +32,8 @@
 
 #pragma once
 
+#include "PluginsErrorDictionary.h"
+
 #if ORTHANC_PLUGINS_ENABLED != 1
 
 #include <boost/noncopyable.hpp>
@@ -40,6 +42,14 @@
 {
   class OrthancPlugins : public boost::noncopyable
   {
+  private:
+    PluginsErrorDictionary  dictionary_;
+
+  public:
+    PluginsErrorDictionary& GetErrorDictionary()
+    {
+      return dictionary_;
+    }
   };
 }
 
@@ -179,6 +189,8 @@
     PluginsManager& GetManager();
 
     const PluginsManager& GetManager() const;
+
+    PluginsErrorDictionary& GetErrorDictionary();
   };
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Engine/PluginsErrorDictionary.cpp	Wed Sep 23 22:05:27 2015 +0200
@@ -0,0 +1,123 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 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/>.
+ **/
+
+
+#include "../../OrthancServer/PrecompiledHeadersServer.h"
+#include "PluginsErrorDictionary.h"
+
+#if ORTHANC_PLUGINS_ENABLED != 1
+#error The plugin support is disabled
+#endif
+
+
+#include "PluginsEnumerations.h"
+#include "PluginsManager.h"
+
+#include <memory>
+
+
+namespace Orthanc
+{
+  PluginsErrorDictionary::PluginsErrorDictionary() : 
+    pos_(ErrorCode_START_PLUGINS)
+  {
+  }
+
+
+  PluginsErrorDictionary::~PluginsErrorDictionary()
+  {
+    for (Errors::iterator it = errors_.begin(); it != errors_.end(); ++it)
+    {
+      delete it->second;
+    }
+  }
+
+
+  OrthancPluginErrorCode PluginsErrorDictionary::Register(SharedLibrary& library,
+                                                          int32_t pluginCode,
+                                                          const char* description,
+                                                          uint16_t httpStatus)
+  {
+    std::auto_ptr<Error> error(new Error);
+
+    error->pluginCode_ = pluginCode;
+    error->description_ = description;
+    error->httpStatus_ = static_cast<HttpStatus>(httpStatus);
+    error->pluginName_ = PluginsManager::GetPluginName(library);
+
+    OrthancPluginErrorCode code;
+
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      errors_[pos_] = error.release();
+      code = static_cast<OrthancPluginErrorCode>(pos_);
+      pos_ += 1;
+    }
+
+    return code;
+  }
+
+
+  void  PluginsErrorDictionary::GetExceptionMessage(Json::Value& message,  /* out */
+                                                    HttpStatus& httpStatus,  /* out */
+                                                    const OrthancException& exception)
+  {
+    bool done = false;
+
+    if (exception.GetErrorCode() >= ErrorCode_START_PLUGINS)
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      Errors::const_iterator error = errors_.find(static_cast<int32_t>(exception.GetErrorCode()));
+      
+      if (error != errors_.end())
+      {
+        httpStatus = error->second->httpStatus_;
+        message["PluginName"] = error->second->pluginName_;
+        message["PluginCode"] = error->second->pluginCode_;
+        message["Message"] = error->second->description_;
+
+        done = true;
+      }
+    }
+
+    if (!done)
+    {
+      httpStatus = exception.GetHttpStatus();
+      message["Message"] = exception.What();
+    }
+
+    message["HttpError"] = EnumerationToString(httpStatus);
+    message["HttpStatus"] = httpStatus;
+    message["OrthancError"] = EnumerationToString(exception.GetErrorCode());
+    message["OrthancStatus"] = exception.GetErrorCode();
+  }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Engine/PluginsErrorDictionary.h	Wed Sep 23 22:05:27 2015 +0200
@@ -0,0 +1,83 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 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 ORTHANC_PLUGINS_ENABLED == 1
+
+#include "../Include/orthanc/OrthancCPlugin.h"
+#include "../../Core/OrthancException.h"
+#include "SharedLibrary.h"
+
+#include <map>
+#include <string>
+#include <boost/noncopyable.hpp>
+#include <boost/thread/mutex.hpp>
+#include <json/value.h>
+
+
+namespace Orthanc
+{
+  class PluginsErrorDictionary : public boost::noncopyable
+  {
+  private:
+    struct Error
+    {
+      int32_t      pluginCode_;
+      std::string  description_;
+      HttpStatus   httpStatus_;
+      std::string  pluginName_;
+    };
+    
+    typedef std::map<int32_t, Error*>  Errors;
+
+    boost::mutex  mutex_;
+    int32_t  pos_;
+    Errors   errors_;
+
+  public:
+    PluginsErrorDictionary();
+
+    ~PluginsErrorDictionary();
+
+    OrthancPluginErrorCode  Register(SharedLibrary& library,
+                                     int32_t  pluginCode,
+                                     const char* description,
+                                     uint16_t httpStatus);
+
+    void  GetExceptionMessage(Json::Value& message,  /* out */
+                              HttpStatus& httpStatus,  /* out */
+                              const OrthancException& exception);
+  };
+}
+
+#endif
--- a/Plugins/Engine/PluginsManager.cpp	Wed Sep 23 14:42:20 2015 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Wed Sep 23 22:05:27 2015 +0200
@@ -346,4 +346,10 @@
       return it->second->GetVersion();
     }
   }
+
+  
+  std::string PluginsManager::GetPluginName(SharedLibrary& library)
+  {
+    return CallGetName(library);
+  }
 }
--- a/Plugins/Engine/PluginsManager.h	Wed Sep 23 14:42:20 2015 +0200
+++ b/Plugins/Engine/PluginsManager.h	Wed Sep 23 22:05:27 2015 +0200
@@ -112,6 +112,8 @@
     bool HasPlugin(const std::string& name) const;
 
     const std::string& GetPluginVersion(const std::string& name) const;
+
+    static std::string GetPluginName(SharedLibrary& library);
   };
 }
 
--- a/Resources/GenerateErrorCodes.py	Wed Sep 23 14:42:20 2015 +0200
+++ b/Resources/GenerateErrorCodes.py	Wed Sep 23 22:05:27 2015 +0200
@@ -33,7 +33,9 @@
 import json
 import os
 import re
+import sys
 
+START_PLUGINS = 1000000
 BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 
 
@@ -45,6 +47,11 @@
 with open(os.path.join(BASE, 'Resources', 'ErrorCodes.json'), 'r') as f:
     ERRORS = json.loads(re.sub('/\*.*?\*/', '', f.read()))
 
+for error in ERRORS:
+    if error['Code'] >= START_PLUGINS:
+        print('ERROR: Error code must be below %d, but "%s" is set to %d' % (START_PLUGINS, error['Name'], error['Code']))
+        sys.exit(-1)
+
 with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'r') as f:
     a = f.read()
 
@@ -63,6 +70,8 @@
     a = f.read()
 
 s = ',\n'.join(map(lambda x: '    ErrorCode_%s = %d    /*!< %s */' % (x['Name'], int(x['Code']), x['Description']), ERRORS))
+
+s += ',\n    ErrorCode_START_PLUGINS = %d' % START_PLUGINS
 a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
 
 with open(path, 'w') as f: