diff Core/OrthancException.h @ 2952:4ceb9bf7b00c

added details string in OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Dec 2018 11:46:04 +0100
parents 878b59270859
children d924f9bb61cc
line wrap: on
line diff
--- a/Core/OrthancException.h	Mon Dec 03 10:02:25 2018 +0100
+++ b/Core/OrthancException.h	Mon Dec 03 11:46:04 2018 +0100
@@ -33,19 +33,39 @@
 
 #pragma once
 
+#include "Enumerations.h"
+#include "Logging.h"
+
 #include <stdint.h>
 #include <string>
-#include "Enumerations.h"
+#include <memory>
 
 namespace Orthanc
 {
   class OrthancException
   {
-  protected:
+  private:
+    OrthancException();  // Forbidden
+    
+    OrthancException& operator= (const OrthancException&);  // Forbidden
+
     ErrorCode  errorCode_;
     HttpStatus httpStatus_;
 
+    // New in Orthanc 1.4.3
+    std::auto_ptr<std::string>  details_;
+    
   public:
+    OrthancException(const OrthancException& other) : 
+      errorCode_(other.errorCode_),
+      httpStatus_(other.httpStatus_)
+    {
+      if (other.details_.get() != NULL)
+      {
+        details_.reset(new std::string(*other.details_));
+      }
+    }
+
     explicit OrthancException(ErrorCode errorCode) : 
       errorCode_(errorCode),
       httpStatus_(ConvertErrorCodeToHttpStatus(errorCode))
@@ -53,12 +73,31 @@
     }
 
     OrthancException(ErrorCode errorCode,
+                     const std::string& details) :
+      errorCode_(errorCode),
+      httpStatus_(ConvertErrorCodeToHttpStatus(errorCode)),
+      details_(new std::string(details))
+    {
+      LOG(ERROR) << EnumerationToString(errorCode_) << ": " << details;
+    }
+
+    OrthancException(ErrorCode errorCode,
                      HttpStatus httpStatus) :
       errorCode_(errorCode),
       httpStatus_(httpStatus)
     {
     }
 
+    OrthancException(ErrorCode errorCode,
+                     HttpStatus httpStatus,
+                     const std::string& details) :
+      errorCode_(errorCode),
+      httpStatus_(httpStatus),
+      details_(new std::string(details))
+    {
+      LOG(ERROR) << EnumerationToString(errorCode_) << ": " << details;
+    }
+
     ErrorCode GetErrorCode() const
     {
       return errorCode_;
@@ -73,5 +112,22 @@
     {
       return EnumerationToString(errorCode_);
     }
+
+    bool HasDetails() const
+    {
+      return details_.get() != NULL;
+    }
+
+    const char* GetDetails() const
+    {
+      if (details_.get() == NULL)
+      {
+        return "";
+      }
+      else
+      {
+        return details_->c_str();
+      }
+    }
   };
 }