changeset 1581:357c4bb15701

Plugins have access to explicit error codes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 16:18:37 +0200
parents bf502300c52e
children bd1889029cbb
files Core/Enumerations.cpp Core/Enumerations.h NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/PluginsManager.cpp Plugins/Engine/PluginsManager.h Plugins/Include/orthanc/OrthancCPlugin.h Resources/ErrorCodes.json
diffstat 8 files changed, 63 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.cpp	Tue Aug 25 15:59:07 2015 +0200
+++ b/Core/Enumerations.cpp	Tue Aug 25 16:18:37 2015 +0200
@@ -130,6 +130,9 @@
       case ErrorCode_SharedLibrary:
         return "Error while using a shared library (plugin)";
 
+      case ErrorCode_UnknownPluginService:
+        return "Plugin invoking an unknown service";
+
       default:
         return "Unknown error code";
     }
--- a/Core/Enumerations.h	Tue Aug 25 15:59:07 2015 +0200
+++ b/Core/Enumerations.h	Tue Aug 25 16:18:37 2015 +0200
@@ -72,7 +72,8 @@
     ErrorCode_ReadOnly = 22    /*!< Cannot modify a read-only data structure */,
     ErrorCode_IncompatibleImageFormat = 23    /*!< Incompatible format of the images */,
     ErrorCode_IncompatibleImageSize = 24    /*!< Incompatible size of the images */,
-    ErrorCode_SharedLibrary = 25    /*!< Error while using a shared library (plugin) */
+    ErrorCode_SharedLibrary = 25    /*!< Error while using a shared library (plugin) */,
+    ErrorCode_UnknownPluginService = 26    /*!< Plugin invoking an unknown service */
   };
 
   enum LogLevel
--- a/NEWS	Tue Aug 25 15:59:07 2015 +0200
+++ b/NEWS	Tue Aug 25 16:18:37 2015 +0200
@@ -19,6 +19,7 @@
 -------
 
 * New function OrthancPluginBufferCompression() to (un)compress memory buffers
+* Plugins have access to explicit error codes 
 
 Maintenance
 -----------
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 25 15:59:07 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 25 16:18:37 2015 +0200
@@ -1199,6 +1199,7 @@
       }
 
       default:
+        // This service is unknown by the Orthanc plugin engine
         return false;
     }
   }
--- a/Plugins/Engine/PluginsManager.cpp	Tue Aug 25 15:59:07 2015 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Tue Aug 25 16:18:37 2015 +0200
@@ -134,30 +134,29 @@
   }
 
 
-  int32_t PluginsManager::InvokeService(OrthancPluginContext* context,
-                                        _OrthancPluginService service, 
-                                        const void* params)
+  OrthancPluginErrorCode PluginsManager::InvokeService(OrthancPluginContext* context,
+                                                       _OrthancPluginService service, 
+                                                       const void* params)
   {
     switch (service)
     {
       case _OrthancPluginService_LogError:
         LOG(ERROR) << reinterpret_cast<const char*>(params);
-        return 0;
+        return OrthancPluginErrorCode_Success;
 
       case _OrthancPluginService_LogWarning:
         LOG(WARNING) << reinterpret_cast<const char*>(params);
-        return 0;
+        return OrthancPluginErrorCode_Success;
 
       case _OrthancPluginService_LogInfo:
         LOG(INFO) << reinterpret_cast<const char*>(params);
-        return 0;
+        return OrthancPluginErrorCode_Success;
 
       default:
         break;
     }
 
     PluginsManager* that = reinterpret_cast<PluginsManager*>(context->pluginsManager);
-    bool error = false;
 
     for (std::list<IPluginServiceProvider*>::iterator
            it = that->serviceProviders_.begin(); 
@@ -167,26 +166,19 @@
       {
         if ((*it)->InvokeService(service, params))
         {
-          return 0;
+          return OrthancPluginErrorCode_Success;
         }
       }
-      catch (OrthancException&)
+      catch (OrthancException& e)
       {
-        // This service provider has failed, go to the next
-        error = true;
+        // This service provider has failed
+        LOG(ERROR) << "Exception while invoking a plugin service: " << e.What();
+        return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
       }
     }
 
-    if (error)
-    {
-      // LOG(ERROR) << "Exception when dealing with service " << service;
-    }
-    else
-    {
-      LOG(ERROR) << "Plugin invoking unknown service " << service;
-    }
-
-    return -1;
+    LOG(ERROR) << "Plugin invoking unknown service: " << service;
+    return OrthancPluginErrorCode_UnknownPluginService;
   }
 
 
--- a/Plugins/Engine/PluginsManager.h	Tue Aug 25 15:59:07 2015 +0200
+++ b/Plugins/Engine/PluginsManager.h	Tue Aug 25 16:18:37 2015 +0200
@@ -76,9 +76,9 @@
     Plugins  plugins_;
     std::list<IPluginServiceProvider*> serviceProviders_;
 
-    static int32_t InvokeService(OrthancPluginContext* context,
-                                 _OrthancPluginService service,
-                                 const void* parameters);
+    static OrthancPluginErrorCode InvokeService(OrthancPluginContext* context,
+                                                _OrthancPluginService service,
+                                                const void* parameters);
 
   public:
     PluginsManager();
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Tue Aug 25 15:59:07 2015 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Tue Aug 25 16:18:37 2015 +0200
@@ -188,6 +188,7 @@
     OrthancPluginErrorCode_IncompatibleImageFormat = 23    /*!< Incompatible format of the images */,
     OrthancPluginErrorCode_IncompatibleImageSize = 24    /*!< Incompatible size of the images */,
     OrthancPluginErrorCode_SharedLibrary = 25    /*!< Error while using a shared library (plugin) */,
+    OrthancPluginErrorCode_UnknownPluginService = 26    /*!< Plugin invoking an unknown service */,
 
     _OrthancPluginErrorCode_INTERNAL = 0x7fffffff
   } OrthancPluginErrorCode;
@@ -607,12 +608,12 @@
    **/
   typedef struct _OrthancPluginContext_t
   {
-    void*              pluginsManager;
-    const char*        orthancVersion;
-    OrthancPluginFree  Free;
-    int32_t          (*InvokeService) (struct _OrthancPluginContext_t* context,
-                                       _OrthancPluginService service,
-                                       const void* params);
+    void*                     pluginsManager;
+    const char*               orthancVersion;
+    OrthancPluginFree         Free;
+    OrthancPluginErrorCode  (*InvokeService) (struct _OrthancPluginContext_t* context,
+                                              _OrthancPluginService service,
+                                              const void* params);
   } OrthancPluginContext;
 
 
@@ -942,9 +943,9 @@
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param target The target memory buffer.
    * @param instanceId The Orthanc identifier of the DICOM instance of interest.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginGetDicomForInstance(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginGetDicomForInstance(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 instanceId)
@@ -972,9 +973,9 @@
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param target The target memory buffer.
    * @param uri The URI in the built-in Orthanc API.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiGet(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiGet(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri)
@@ -999,9 +1000,9 @@
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param target The target memory buffer.
    * @param uri The URI in the built-in Orthanc API.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiGetAfterPlugins(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiGetAfterPlugins(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri)
@@ -1033,9 +1034,9 @@
    * @param uri The URI in the built-in Orthanc API.
    * @param body The body of the POST request.
    * @param bodySize The size of the body.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiPost(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiPost(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri,
@@ -1065,9 +1066,9 @@
    * @param uri The URI in the built-in Orthanc API.
    * @param body The body of the POST request.
    * @param bodySize The size of the body.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiPostAfterPlugins(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiPostAfterPlugins(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri,
@@ -1091,9 +1092,9 @@
    * 
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param uri The URI to delete in the built-in Orthanc API.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiDelete(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiDelete(
     OrthancPluginContext*       context,
     const char*                 uri)
   {
@@ -1111,9 +1112,9 @@
    * 
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param uri The URI to delete in the built-in Orthanc API.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiDeleteAfterPlugins(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiDeleteAfterPlugins(
     OrthancPluginContext*       context,
     const char*                 uri)
   {
@@ -1133,9 +1134,9 @@
    * @param uri The URI in the built-in Orthanc API.
    * @param body The body of the PUT request.
    * @param bodySize The size of the body.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiPut(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiPut(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri,
@@ -1166,9 +1167,9 @@
    * @param uri The URI in the built-in Orthanc API.
    * @param body The body of the PUT request.
    * @param bodySize The size of the body.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int  OrthancPluginRestApiPutAfterPlugins(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiPutAfterPlugins(
     OrthancPluginContext*       context,
     OrthancPluginMemoryBuffer*  target,
     const char*                 uri,
@@ -2076,9 +2077,9 @@
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param property The global property of interest.
    * @param value The value to be set in the global property.
-   * @return 0 if success, -1 in case of error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginSetGlobalProperty(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginSetGlobalProperty(
     OrthancPluginContext*  context,
     int32_t                property,
     const char*            value)
@@ -2088,15 +2089,7 @@
     params.property = property;
     params.value = value;
 
-    if (context->InvokeService(context, _OrthancPluginService_SetGlobalProperty, &params))
-    {
-      /* Error */
-      return -1;
-    }
-    else
-    {
-      return 0;
-    }
+    return context->InvokeService(context, _OrthancPluginService_SetGlobalProperty, &params);
   }
 
 
@@ -2252,10 +2245,10 @@
    * @param output The HTTP connection to the client application.
    * @param subType The sub-type of the multipart answer ("mixed" or "related").
    * @param contentType The MIME type of the items in the multipart answer.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    * @see OrthancPluginSendMultipartItem()
    **/
-  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginStartMultipartAnswer(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginStartMultipartAnswer(
     OrthancPluginContext*    context,
     OrthancPluginRestOutput* output,
     const char*              subType,
@@ -2279,10 +2272,10 @@
    * @param output The HTTP connection to the client application.
    * @param answer Pointer to the memory buffer containing the item.
    * @param answerSize Number of bytes of the item.
-   * @return 0 if success, other value if error (this notably happens
+   * @return 0 if success, or the error code if failure (this notably happens
    * if the connection is closed by the client).
    **/
-  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginSendMultipartItem(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginSendMultipartItem(
     OrthancPluginContext*    context,
     OrthancPluginRestOutput* output,
     const char*              answer,
@@ -2321,9 +2314,9 @@
    * @param compression The compression algorithm.
    * @param uncompress If set to "0", the buffer must be compressed. 
    * If set to "1", the buffer must be uncompressed.
-   * @return 0 if success, other value if error.
+   * @return 0 if success, or the error code if failure.
    **/
-  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginBufferCompression(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginBufferCompression(
     OrthancPluginContext*         context,
     OrthancPluginMemoryBuffer*    target,
     const void*                   source,
--- a/Resources/ErrorCodes.json	Tue Aug 25 15:59:07 2015 +0200
+++ b/Resources/ErrorCodes.json	Tue Aug 25 16:18:37 2015 +0200
@@ -121,7 +121,7 @@
     {
         "Code": 20, 
         "Name": "CorruptedFile", 
-        "Description": "Corrupted file (inconsistent MD5 hash)"
+        "Description": "Corrupted file (e.g. inconsistent MD5 hash)"
     }, 
     {
         "Code": 21, 
@@ -148,5 +148,10 @@
         "Code": 25, 
         "Name": "SharedLibrary", 
         "Description": "Error while using a shared library (plugin)"
+    }, 
+    {
+        "Code": 26, 
+        "Name": "UnknownPluginService", 
+        "Description": "Plugin invoking an unknown service"
     }
 ]