changeset 1628:77c4cc4def0f

OrthancPluginErrorCode in REST callbacks
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 18 Sep 2015 16:46:35 +0200
parents da7854deb662
children bad4772b605c
files Plugins/Engine/OrthancPlugins.cpp Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Samples/GdcmDecoding/Plugin.cpp Plugins/Samples/ServeFolders/Plugin.cpp Plugins/Samples/WebSkeleton/Framework/Plugin.cpp
diffstat 5 files changed, 47 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Engine/OrthancPlugins.cpp	Fri Sep 18 16:32:29 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Sep 18 16:46:35 2015 +0200
@@ -64,9 +64,9 @@
       OrthancPluginRestCallback callback_;
       bool                      lock_;
 
-      int32_t InvokeInternal(HttpOutput& output,
-                             const std::string& flatUri,
-                             const OrthancPluginHttpRequest& request)
+      OrthancPluginErrorCode InvokeInternal(HttpOutput& output,
+                                            const std::string& flatUri,
+                                            const OrthancPluginHttpRequest& request)
       {
         return callback_(reinterpret_cast<OrthancPluginRestOutput*>(&output), 
                          flatUri.c_str(), 
@@ -88,10 +88,10 @@
         return regex_;
       }
 
-      int32_t Invoke(boost::recursive_mutex& restCallbackMutex,
-                     HttpOutput& output,
-                     const std::string& flatUri,
-                     const OrthancPluginHttpRequest& request)
+      OrthancPluginErrorCode Invoke(boost::recursive_mutex& restCallbackMutex,
+                                    HttpOutput& output,
+                                    const std::string& flatUri,
+                                    const OrthancPluginHttpRequest& request)
       {
         if (lock_)
         {
@@ -335,27 +335,21 @@
     }
 
     assert(callback != NULL);
-    int32_t error = callback->Invoke(pimpl_->restCallbackMutex_, output, flatUri, request);
+    OrthancPluginErrorCode error = callback->Invoke(pimpl_->restCallbackMutex_, output, flatUri, request);
 
-    if (error == 0 && 
+    if (error == OrthancPluginErrorCode_Success && 
         output.IsWritingMultipart())
     {
       output.CloseMultipart();
     }
 
-    if (error < 0)
+    if (error == OrthancPluginErrorCode_Success)
     {
-      LOG(ERROR) << "Plugin callback failed with error code " << error;
-      return false;
+      return true;
     }
     else
     {
-      if (error > 0)
-      {
-        LOG(WARNING) << "Plugin callback finished with warning code " << error;
-      }
-
-      return true;
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Sep 18 16:32:29 2015 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Sep 18 16:46:35 2015 +0200
@@ -652,7 +652,7 @@
    * @brief Signature of a callback function that answers to a REST request.
    * @ingroup Callbacks
    **/
-  typedef int32_t (*OrthancPluginRestCallback) (
+  typedef OrthancPluginErrorCode (*OrthancPluginRestCallback) (
     OrthancPluginRestOutput* output,
     const char* url,
     const OrthancPluginHttpRequest* request);
--- a/Plugins/Samples/GdcmDecoding/Plugin.cpp	Fri Sep 18 16:32:29 2015 +0200
+++ b/Plugins/Samples/GdcmDecoding/Plugin.cpp	Fri Sep 18 16:46:35 2015 +0200
@@ -85,9 +85,9 @@
 }
 
 
-ORTHANC_PLUGINS_API int32_t DecodeImage(OrthancPluginRestOutput* output,
-                                        const char* url,
-                                        const OrthancPluginHttpRequest* request)
+ORTHANC_PLUGINS_API OrthancPluginErrorCode DecodeImage(OrthancPluginRestOutput* output,
+                                                       const char* url,
+                                                       const OrthancPluginHttpRequest* request)
 {
   std::string instance(request->groups[0]);
   std::string outputFormat(request->groups[1]);
@@ -107,7 +107,7 @@
   {
     OrthancContext::GetInstance().LogError("GDCM cannot extract an image from this DICOM instance");
     AnswerUnsupportedImage(output);
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   gdcm::Image& image = imageReader.GetImage();
@@ -138,7 +138,7 @@
   {
     OrthancContext::GetInstance().LogError("This sample plugin does not support this image format");
     AnswerUnsupportedImage(output);
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   Orthanc::ImageAccessor decodedImage;
@@ -190,7 +190,7 @@
     {
       // Do not convert color images to grayscale values (this is Orthanc convention)
       AnswerUnsupportedImage(output);
-      return 0;
+      return OrthancPluginErrorCode_Success;
     }
 
     if (outputFormat == "image-uint8")
@@ -209,7 +209,7 @@
     {
       OrthancContext::GetInstance().LogError("Unknown output format: " + outputFormat);
       AnswerUnsupportedImage(output);
-      return 0;
+      return OrthancPluginErrorCode_Success;
     }
   }
 
@@ -219,7 +219,7 @@
   // Compress the converted image as a PNG file
   OrthancContext::GetInstance().CompressAndAnswerPngImage(output, convertedAccessor);
 
-  return 0;  // Success
+  return OrthancPluginErrorCode_Success;  // Success
 }
 
 
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Sep 18 16:32:29 2015 +0200
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Sep 18 16:46:35 2015 +0200
@@ -156,16 +156,16 @@
 }
 
 
-static int32_t FolderCallback(OrthancPluginRestOutput* output,
-                              const char* url,
-                              const OrthancPluginHttpRequest* request)
+static OrthancPluginErrorCode FolderCallback(OrthancPluginRestOutput* output,
+                                             const char* url,
+                                             const OrthancPluginHttpRequest* request)
 {
   namespace fs = boost::filesystem;  
 
   if (request->method != OrthancPluginHttpMethod_Get)
   {
     OrthancPluginSendMethodNotAllowed(context_, output, "GET");
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   std::string folder;
@@ -231,18 +231,18 @@
     }
   }
 
-  return 0;
+  return OrthancPluginErrorCode_Success;
 }
 
 
-static int32_t ListServedFolders(OrthancPluginRestOutput* output,
-                                 const char* url,
-                                 const OrthancPluginHttpRequest* request)
+static OrthancPluginErrorCode ListServedFolders(OrthancPluginRestOutput* output,
+                                                const char* url,
+                                                const OrthancPluginHttpRequest* request)
 {
   if (request->method != OrthancPluginHttpMethod_Get)
   {
     OrthancPluginSendMethodNotAllowed(context_, output, "GET");
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n";
@@ -268,7 +268,7 @@
 
   OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
 
-  return 0;
+  return OrthancPluginErrorCode_Success;
 }
 
 
--- a/Plugins/Samples/WebSkeleton/Framework/Plugin.cpp	Fri Sep 18 16:32:29 2015 +0200
+++ b/Plugins/Samples/WebSkeleton/Framework/Plugin.cpp	Fri Sep 18 16:46:35 2015 +0200
@@ -131,14 +131,14 @@
 
 
 #if ORTHANC_PLUGIN_STANDALONE == 1
-static int32_t ServeStaticResource(OrthancPluginRestOutput* output,
-                                   const char* url,
-                                   const OrthancPluginHttpRequest* request)
+static OrthancPluginErrorCode ServeStaticResource(OrthancPluginRestOutput* output,
+                                                  const char* url,
+                                                  const OrthancPluginHttpRequest* request)
 {
   if (request->method != OrthancPluginHttpMethod_Get)
   {
     OrthancPluginSendMethodNotAllowed(context, output, "GET");
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   std::string path = "/" + std::string(request->groups[0]);
@@ -152,29 +152,28 @@
 
     const char* resource = s.size() ? s.c_str() : NULL;
     OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
-
-    return 0;
   }
   catch (std::runtime_error&)
   {
     std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]);
     OrthancPluginLogError(context, s.c_str());
     OrthancPluginSendHttpStatusCode(context, output, 404);
-    return 0;
   }
+
+  return OrthancPluginErrorCode_Success;
 }
 #endif
 
 
 #if ORTHANC_PLUGIN_STANDALONE == 0
-static int32_t ServeFolder(OrthancPluginRestOutput* output,
-                           const char* url,
-                           const OrthancPluginHttpRequest* request)
+static OrthancPluginErrorCode ServeFolder(OrthancPluginRestOutput* output,
+                                          const char* url,
+                                          const OrthancPluginHttpRequest* request)
 {
   if (request->method != OrthancPluginHttpMethod_Get)
   {
     OrthancPluginSendMethodNotAllowed(context, output, "GET");
-    return 0;
+    return OrthancPluginErrorCode_Success;
   }
 
   std::string path = ORTHANC_PLUGIN_RESOURCES_ROOT "/" + std::string(request->groups[0]);
@@ -185,23 +184,22 @@
   {
     const char* resource = s.size() ? s.c_str() : NULL;
     OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
-
-    return 0;
   }
   else
   {
     std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]);
     OrthancPluginLogError(context, s.c_str());
     OrthancPluginSendHttpStatusCode(context, output, 404);
-    return 0;
   }
+
+  return OrthancPluginErrorCode_Success;
 }
 #endif
 
 
-static int32_t RedirectRoot(OrthancPluginRestOutput* output,
-                            const char* url,
-                            const OrthancPluginHttpRequest* request)
+static OrthancPluginErrorCode RedirectRoot(OrthancPluginRestOutput* output,
+                                           const char* url,
+                                           const OrthancPluginHttpRequest* request)
 {
   if (request->method != OrthancPluginHttpMethod_Get)
   {
@@ -212,7 +210,7 @@
     OrthancPluginRedirect(context, output, ORTHANC_PLUGIN_WEB_ROOT "index.html");
   }
 
-  return 0;
+  return OrthancPluginErrorCode_Success;
 }