diff OrthancServer/main.cpp @ 1645:1558b3226b18

IHttpExceptionFormatter
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 24 Sep 2015 15:55:17 +0200
parents eb8fbcf008b5
children 8040d56cb0b3
line wrap: on
line diff
--- a/OrthancServer/main.cpp	Wed Sep 23 22:05:27 2015 +0200
+++ b/OrthancServer/main.cpp	Thu Sep 24 15:55:17 2015 +0200
@@ -307,6 +307,67 @@
 };
 
 
+
+class MyHttpExceptionFormatter : public IHttpExceptionFormatter
+{
+private:
+  bool             describeErrors_;
+  OrthancPlugins*  plugins_;
+
+public:
+  MyHttpExceptionFormatter(bool describeErrors,
+                           OrthancPlugins* plugins) :
+    describeErrors_(describeErrors),
+    plugins_(plugins)
+  {
+  }
+
+  virtual void Format(HttpOutput& output,
+                      const OrthancException& exception,
+                      HttpMethod method,
+                      const char* uri)
+  {
+    if (!describeErrors_)
+    {
+      output.SendStatus(exception.GetHttpStatus());
+      return;
+    }
+
+    Json::Value message = Json::objectValue;
+    message["Method"] = EnumerationToString(method);
+    message["Uri"] = uri;
+
+    ErrorCode errorCode = exception.GetErrorCode();
+    HttpStatus httpStatus = exception.GetHttpStatus();
+
+    bool isPlugin = false;
+
+#if ORTHANC_PLUGINS_ENABLED == 1
+    if (plugins_ != NULL &&
+        plugins_->GetErrorDictionary().Format(message, httpStatus, exception))
+    {
+      errorCode = ErrorCode_Plugin;
+      isPlugin = true;
+    }
+#endif
+
+    if (!isPlugin)
+    {
+      message["Message"] = exception.What();
+    }
+
+    message["HttpError"] = EnumerationToString(httpStatus);
+    message["HttpStatus"] = httpStatus;
+    message["OrthancError"] = EnumerationToString(errorCode);
+    message["OrthancStatus"] = errorCode;
+
+    std::string info = message.toStyledString();
+    output.SendStatus(httpStatus, info);
+  }
+};
+
+
+
 static void PrintHelp(char* path)
 {
   std::cout 
@@ -413,7 +474,8 @@
 
 
 static bool StartHttpServer(ServerContext& context,
-                            OrthancRestApi& restApi)
+                            OrthancRestApi& restApi,
+                            OrthancPlugins* plugins)
 {
   if (!Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
   {
@@ -421,6 +483,9 @@
     return WaitForExit(context, restApi);
   }
 
+  MyHttpExceptionFormatter exceptionFormatter(Configuration::GetGlobalBoolParameter("HttpDescribeErrors", true), plugins);
+  
+
   // HTTP server
   MyIncomingHttpRequestFilter httpFilter(context);
   MongooseServer httpServer;
@@ -428,8 +493,8 @@
   httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false));
   httpServer.SetKeepAliveEnabled(Configuration::GetGlobalBoolParameter("KeepAlive", false));
   httpServer.SetHttpCompressionEnabled(Configuration::GetGlobalBoolParameter("HttpCompressionEnabled", true));
-  httpServer.SetDescribeErrorsEnabled(Configuration::GetGlobalBoolParameter("HttpDescribeErrors", true));
   httpServer.SetIncomingHttpRequestFilter(httpFilter);
+  httpServer.SetHttpExceptionFormatter(exceptionFormatter);
 
   httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false));
   Configuration::SetupRegisteredUsers(httpServer);
@@ -461,12 +526,13 @@
 
 
 static bool StartDicomServer(ServerContext& context,
-                             OrthancRestApi& restApi)
+                             OrthancRestApi& restApi,
+                             OrthancPlugins* plugins)
 {
   if (!Configuration::GetGlobalBoolParameter("DicomServerEnabled", true))
   {
     LOG(WARNING) << "The DICOM server is disabled";
-    return StartHttpServer(context, restApi);
+    return StartHttpServer(context, restApi, plugins);
   }
 
   MyDicomServerFactory serverFactory(context);
@@ -485,7 +551,7 @@
   dicomServer.Start();
   LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
 
-  bool restart = StartHttpServer(context, restApi);
+  bool restart = StartHttpServer(context, restApi, plugins);
 
   dicomServer.Stop();
   LOG(WARNING) << "    DICOM server has stopped";
@@ -522,7 +588,7 @@
   OrthancRestApi restApi(context);
   context.GetHttpHandler().Register(restApi, true);
 
-  return StartDicomServer(context, restApi);
+  return StartDicomServer(context, restApi, plugins);
 }