changeset 1567:9c5d93510414

If error while calling the REST API, the answer body contains an error description
author jodogne
date Sun, 23 Aug 2015 09:49:16 +0200
parents 3be6eb3757c8
children 818ae9bc493a
files Core/HttpServer/HttpOutput.cpp Core/HttpServer/HttpOutput.h Core/HttpServer/MongooseServer.cpp Core/RestApi/RestApiOutput.cpp Core/RestApi/RestApiOutput.h NEWS
diffstat 6 files changed, 44 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/HttpOutput.cpp	Fri Aug 21 17:37:13 2015 +0200
+++ b/Core/HttpServer/HttpOutput.cpp	Sun Aug 23 09:49:16 2015 +0200
@@ -287,7 +287,9 @@
   }
 
 
-  void HttpOutput::SendStatus(HttpStatus status)
+  void HttpOutput::SendStatus(HttpStatus status,
+			      const char* message,
+			      size_t messageSize)
   {
     if (status == HttpStatus_200_Ok ||
         status == HttpStatus_301_MovedPermanently ||
@@ -300,7 +302,7 @@
     
     stateMachine_.ClearHeaders();
     stateMachine_.SetHttpStatus(status);
-    stateMachine_.SendBody(NULL, 0);
+    stateMachine_.SendBody(message, messageSize);
   }
 
 
--- a/Core/HttpServer/HttpOutput.h	Fri Aug 21 17:37:13 2015 +0200
+++ b/Core/HttpServer/HttpOutput.h	Sun Aug 23 09:49:16 2015 +0200
@@ -146,7 +146,20 @@
       return isGzipAllowed_;
     }
 
-    void SendStatus(HttpStatus status);
+    void SendStatus(HttpStatus status,
+		    const char* message,
+		    size_t messageSize);
+
+    void SendStatus(HttpStatus status)
+    {
+      SendStatus(status, NULL, 0);
+    }
+
+    void SendStatus(HttpStatus status,
+		    const std::string& message)
+    {
+      SendStatus(status, message.c_str(), message.size());
+    }
 
     void SetContentType(const char* contentType)
     {
--- a/Core/HttpServer/MongooseServer.cpp	Fri Aug 21 17:37:13 2015 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Sun Aug 23 09:49:16 2015 +0200
@@ -749,7 +749,7 @@
             break;
 
           default:
-            output.SendStatus(HttpStatus_500_InternalServerError);
+            output.SendStatus(HttpStatus_500_InternalServerError, e.What());
         }
       }
       catch (OrthancException&)
@@ -763,13 +763,13 @@
     catch (boost::bad_lexical_cast&)
     {
       LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast";
-      output.SendStatus(HttpStatus_400_BadRequest);
+      output.SendStatus(HttpStatus_400_BadRequest, "Cannot cast some argument");
       return;
     }
     catch (std::runtime_error&)
     {
       LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request";
-      output.SendStatus(HttpStatus_400_BadRequest);
+      output.SendStatus(HttpStatus_400_BadRequest, "Bad JSON request");
       return;
     }
 
--- a/Core/RestApi/RestApiOutput.cpp	Fri Aug 21 17:37:13 2015 +0200
+++ b/Core/RestApi/RestApiOutput.cpp	Sun Aug 23 09:49:16 2015 +0200
@@ -135,7 +135,9 @@
     alreadySent_ = true;
   }
 
-  void RestApiOutput::SignalError(HttpStatus status)
+  void RestApiOutput::SignalErrorInternal(HttpStatus status,
+					  const char* message,
+					  size_t messageSize)
   {
     if (status != HttpStatus_400_BadRequest &&
         status != HttpStatus_403_Forbidden &&
@@ -146,10 +148,21 @@
     }
 
     CheckStatus();
-    output_.SendStatus(status);
+    output_.SendStatus(status, message, messageSize);
     alreadySent_ = true;    
   }
 
+  void RestApiOutput::SignalError(HttpStatus status)
+  {
+    SignalErrorInternal(status, NULL, 0);
+  }
+
+  void RestApiOutput::SignalError(HttpStatus status,
+				  const std::string& message)
+  {
+    SignalErrorInternal(status, message.c_str(), message.size());
+  }
+
   void RestApiOutput::SetCookie(const std::string& name,
                                 const std::string& value,
                                 unsigned int maxAge)
--- a/Core/RestApi/RestApiOutput.h	Fri Aug 21 17:37:13 2015 +0200
+++ b/Core/RestApi/RestApiOutput.h	Sun Aug 23 09:49:16 2015 +0200
@@ -49,6 +49,10 @@
 
     void CheckStatus();
 
+    void SignalErrorInternal(HttpStatus status,
+			     const char* message,
+			     size_t messageSize);
+
   public:
     RestApiOutput(HttpOutput& output,
                   HttpMethod method);
@@ -78,6 +82,9 @@
 
     void SignalError(HttpStatus status);
 
+    void SignalError(HttpStatus status,
+		     const std::string& message);
+
     void Redirect(const std::string& path);
 
     void SetCookie(const std::string& name,
--- a/NEWS	Fri Aug 21 17:37:13 2015 +0200
+++ b/NEWS	Sun Aug 23 09:49:16 2015 +0200
@@ -17,6 +17,7 @@
 -----------
 
 * Many code refactorings
+* If error while calling the REST API, the answer body contains an error description
 * Upgrade to curl 7.44.0 for static and Windows builds
 * Upgrade to libcurl 1.0.2d for static and Windows builds
 * Bypass zlib uncompression if "StorageCompression" is enabled and HTTP client supports deflate