changeset 3345:f687e11aeb13

more explicit HTTP errors
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Apr 2019 17:49:29 +0200
parents 90b4a5001c24
children f509d3c6d570
files OrthancServer/OrthancRestApi/OrthancRestModalities.cpp
diffstat 1 files changed, 24 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Fri Apr 05 17:04:28 2019 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Fri Apr 05 17:49:29 2019 +0200
@@ -442,10 +442,24 @@
     ServerContext& context = OrthancRestApi::GetContext(call);
     Json::Value request;
 
-    if (call.ParseJsonRequest(request) &&
-        request.type() == Json::objectValue &&
-        request.isMember(KEY_LEVEL) && request[KEY_LEVEL].type() == Json::stringValue &&
-        (!request.isMember(KEY_QUERY) || request[KEY_QUERY].type() == Json::objectValue))
+    if (!call.ParseJsonRequest(request) ||
+        request.type() != Json::objectValue)
+    {
+      throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON object");
+    }
+    else if (!request.isMember(KEY_LEVEL) ||
+             request[KEY_LEVEL].type() != Json::stringValue)
+    {
+      throw OrthancException(ErrorCode_BadFileFormat,
+                             "The JSON body must contain field " + std::string(KEY_LEVEL));
+    }
+    else if (request.isMember(KEY_QUERY) &&
+             request[KEY_QUERY].type() != Json::objectValue)
+    {
+      throw OrthancException(ErrorCode_BadFileFormat,
+                             "The field " + std::string(KEY_QUERY) + " must contain a JSON object");
+    }
+    else
     {
       std::auto_ptr<QueryRetrieveHandler>  handler(new QueryRetrieveHandler(context));
 
@@ -958,7 +972,8 @@
         request[KEY_RESOURCES].type() != Json::arrayValue ||
         request[KEY_LEVEL].type() != Json::stringValue)
     {
-      throw OrthancException(ErrorCode_BadFileFormat);
+      throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON body containing fields " +
+                             std::string(KEY_RESOURCES) + " and " + std::string(KEY_LEVEL));
     }
 
     ResourceType level = StringToResourceType(request[KEY_LEVEL].asCString());
@@ -1236,6 +1251,10 @@
       answers.ToJson(result, true);
       call.GetOutput().AnswerJson(result);
     }
+    else
+    {
+      throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON object");
+    }
   }