diff Core/HttpServer/MongooseServer.cpp @ 896:c4053ac5db04 plugins

better plugni api
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 17 Jun 2014 09:57:02 +0200
parents 7e8cde5905fd
children bb0a51561016
line wrap: on
line diff
--- a/Core/HttpServer/MongooseServer.cpp	Mon Jun 16 17:47:58 2014 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Tue Jun 17 09:57:02 2014 +0200
@@ -689,7 +689,7 @@
       }
 
 
-      // Call the proper handler for this URI
+      // Decompose the URI into its components
       UriComponents uri;
       try
       {
@@ -702,44 +702,61 @@
       }
 
 
+      // Locate the candidate handlers for this URI
+      LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri);
       std::list<HttpHandler*> handlers;
       that->FindHandlers(handlers, uri);
 
       bool found = false;
+      bool isError = false;
+      HttpStatus errorStatus;
+      std::string errorDescription;
 
+
+      // Loop over the candidate handlers for this URI
       for (std::list<HttpHandler*>::const_iterator
              it = handlers.begin(); it != handlers.end() && !found; it++)
       {
         try
         {
-          LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri);
           found = (*it)->Handle(output, method, uri, headers, argumentsGET, body);
         }
         catch (OrthancException& e)
         {
-          LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]";
-          output.SendHeader(HttpStatus_500_InternalServerError);
-          found = true;
+          // Using this candidate handler results in an exception, try
+          // another handler before failing
+          isError = true;
+          errorStatus = HttpStatus_500_InternalServerError;
+          errorDescription = e.What();
         }
         catch (boost::bad_lexical_cast&)
         {
-          LOG(ERROR) << "MongooseServer Exception: Bad lexical cast";
-          output.SendHeader(HttpStatus_400_BadRequest);
-          found = true;
+          isError = true;
+          errorStatus = HttpStatus_400_BadRequest;
+          errorDescription = "Bad lexical cast";
         }
         catch (std::runtime_error&)
         {
-          LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request";
-          output.SendHeader(HttpStatus_400_BadRequest);
-          found = true;
+          isError = true;
+          errorStatus = HttpStatus_400_BadRequest;
+          errorDescription = "Presumably a bad JSON request";
         }
       }
       
       if (!found)
       {
-        output.SendHeader(HttpStatus_404_NotFound);
+        if (isError)
+        {
+          LOG(ERROR) << "Exception in the HTTP handler: " << errorDescription;
+          output.SendHeader(errorStatus);
+        }
+        else
+        {
+          output.SendHeader(HttpStatus_404_NotFound);
+        }
       }
 
+
       // Mark as processed
       return (void*) "";
     }