changeset 1576:de54c19fc44d

refactoring OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 14:48:06 +0200
parents f8aae45011c9
children 7aac0cddd42e
files CMakeLists.txt Core/Enumerations.cpp Core/Enumerations.h Core/OrthancException.cpp Core/OrthancException.h Resources/DicomConformanceStatement.py Resources/ErrorCodes.json Resources/GenerateErrorCodes.py
diffstat 8 files changed, 298 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Aug 25 14:00:57 2015 +0200
+++ b/CMakeLists.txt	Tue Aug 25 14:48:06 2015 +0200
@@ -87,7 +87,6 @@
   Core/Compression/HierarchicalZipWriter.cpp
   Core/Compression/ZipWriter.cpp
   Core/Compression/ZlibCompressor.cpp
-  Core/OrthancException.cpp
   Core/DicomFormat/DicomArray.cpp
   Core/DicomFormat/DicomMap.cpp
   Core/DicomFormat/DicomTag.cpp
--- a/Core/Enumerations.cpp	Tue Aug 25 14:00:57 2015 +0200
+++ b/Core/Enumerations.cpp	Tue Aug 25 14:48:06 2015 +0200
@@ -919,4 +919,39 @@
         throw OrthancException(ErrorCode_ParameterOutOfRange);
     }
   }
+
+
+  HttpStatus ConvertErrorCodeToHttpStatus(ErrorCode error)
+  {
+    switch (error)
+    {
+      case ErrorCode_Success:
+      {
+        return HttpStatus_200_Ok;
+      }
+
+      case ErrorCode_InexistentFile:
+      case ErrorCode_InexistentItem:
+      case ErrorCode_InexistentTag:
+      case ErrorCode_UnknownResource:
+      {
+        return HttpStatus_404_NotFound;
+      }
+
+      case ErrorCode_BadFileFormat:
+      case ErrorCode_BadParameterType:
+      case ErrorCode_BadRequest:
+      case ErrorCode_ParameterOutOfRange:
+      case ErrorCode_UriSyntax:
+      {
+        return HttpStatus_400_BadRequest;
+        break;
+      }
+
+      default:
+      {
+        return HttpStatus_500_InternalServerError;
+      }
+    }
+  }
 }
--- a/Core/Enumerations.h	Tue Aug 25 14:00:57 2015 +0200
+++ b/Core/Enumerations.h	Tue Aug 25 14:48:06 2015 +0200
@@ -401,4 +401,6 @@
   DicomModule GetModule(ResourceType type);
 
   const char* GetDicomSpecificCharacterSet(Encoding encoding);
+
+  HttpStatus ConvertErrorCodeToHttpStatus(ErrorCode error);
 }
--- a/Core/OrthancException.cpp	Tue Aug 25 14:00:57 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,90 +0,0 @@
-/**
- * Orthanc - A Lightweight, RESTful DICOM Store
- * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
- * Department, University Hospital of Liege, Belgium
- *
- * This program is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * In addition, as a special exception, the copyright holders of this
- * program give permission to link the code of its release with the
- * OpenSSL project's "OpenSSL" library (or with modified versions of it
- * that use the same license as the "OpenSSL" library), and distribute
- * the linked executables. You must obey the GNU General Public License
- * in all respects for all of the code used other than "OpenSSL". If you
- * modify file(s) with this exception, you may extend this exception to
- * your version of the file(s), but you are not obligated to do so. If
- * you do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source files
- * in the program, then also delete it here.
- * 
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- **/
-
-
-#include "PrecompiledHeaders.h"
-#include "OrthancException.h"
-
-namespace Orthanc
-{
-  HttpStatus OrthancException::ConvertToHttpStatus(ErrorCode code)
-  {
-    switch (code)
-    {
-      case ErrorCode_Success:
-      {
-        return HttpStatus_200_Ok;
-      }
-
-      case ErrorCode_InexistentFile:
-      case ErrorCode_InexistentItem:
-      case ErrorCode_InexistentTag:
-      case ErrorCode_UnknownResource:
-      {
-        return HttpStatus_404_NotFound;
-      }
-
-      case ErrorCode_BadFileFormat:
-      case ErrorCode_BadParameterType:
-      case ErrorCode_BadRequest:
-      case ErrorCode_ParameterOutOfRange:
-      case ErrorCode_UriSyntax:
-      {
-        return HttpStatus_400_BadRequest;
-        break;
-      }
-
-      default:
-      {
-        return HttpStatus_500_InternalServerError;
-      }
-    }
-  }
-
-  const char* OrthancException::What() const
-  {
-    if (errorCode_ == ErrorCode_Custom)
-    {
-      return custom_.c_str();
-    }
-    else
-    {
-      return EnumerationToString(errorCode_);
-    }
-  }
-
-
-  OrthancException::OrthancException(ErrorCode errorCode) : 
-    errorCode_(errorCode),
-    httpStatus_(ConvertToHttpStatus(errorCode))
-  {
-  }
-}
--- a/Core/OrthancException.h	Tue Aug 25 14:00:57 2015 +0200
+++ b/Core/OrthancException.h	Tue Aug 25 14:48:06 2015 +0200
@@ -61,7 +61,11 @@
     {
     }
 
-    OrthancException(ErrorCode errorCode);
+    OrthancException(ErrorCode errorCode) : 
+      errorCode_(errorCode),
+      httpStatus_(ConvertErrorCodeToHttpStatus(errorCode))
+    {
+    }
 
     OrthancException(ErrorCode errorCode,
                      HttpStatus httpStatus) :
@@ -80,6 +84,16 @@
       return httpStatus_;
     }
 
-    const char* What() const;
+    const char* What() const
+    {
+      if (errorCode_ == ErrorCode_Custom)
+      {
+        return custom_.c_str();
+      }
+      else
+      {
+        return EnumerationToString(errorCode_);
+      }
+    }
   };
 }
--- a/Resources/DicomConformanceStatement.py	Tue Aug 25 14:00:57 2015 +0200
+++ b/Resources/DicomConformanceStatement.py	Tue Aug 25 14:48:06 2015 +0200
@@ -1,5 +1,37 @@
 #!/usr/bin/python
 
+# Orthanc - A Lightweight, RESTful DICOM Store
+# Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+# Department, University Hospital of Liege, Belgium
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# In addition, as a special exception, the copyright holders of this
+# program give permission to link the code of its release with the
+# OpenSSL project's "OpenSSL" library (or with modified versions of it
+# that use the same license as the "OpenSSL" library), and distribute
+# the linked executables. You must obey the GNU General Public License
+# in all respects for all of the code used other than "OpenSSL". If you
+# modify file(s) with this exception, you may extend this exception to
+# your version of the file(s), but you are not obligated to do so. If
+# you do not wish to do so, delete this exception statement from your
+# version. If you delete this exception statement from all source files
+# in the program, then also delete it here.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+
+
 # This file injects the UID information into the DICOM conformance
 # statement of Orthanc
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/ErrorCodes.json	Tue Aug 25 14:48:06 2015 +0200
@@ -0,0 +1,152 @@
+[
+    {
+        "Code": -1, 
+        "Name": "Custom", 
+        "Description": "Custom error, see the attached error message"
+    }, 
+    {
+        "Code": 0, 
+        "HttpStatus": 200, 
+        "Name": "Success", 
+        "Description": "Success"
+    }, 
+    {
+        "Code": 1, 
+        "Name": "InternalError", 
+        "Description": "Internal error"
+    }, 
+    {
+        "Code": 2, 
+        "Name": "NotImplemented", 
+        "Description": "Not implemented yet"
+    }, 
+    {
+        "Code": 3, 
+        "HttpStatus": 400, 
+        "Name": "ParameterOutOfRange", 
+        "Description": "Parameter out of range"
+    }, 
+    {
+        "Code": 4, 
+        "Name": "NotEnoughMemory", 
+        "Description": "Not enough memory"
+    }, 
+    {
+        "Code": 5, 
+        "HttpStatus": 400, 
+        "Name": "BadParameterType", 
+        "Description": "Bad type for a parameter"
+    }, 
+    {
+        "Code": 6, 
+        "Name": "BadSequenceOfCalls", 
+        "Description": "Bad sequence of calls"
+    }, 
+    {
+        "Code": 7, 
+        "HttpStatus": 404, 
+        "Name": "InexistentItem", 
+        "Description": "Accessing an inexistent item"
+    }, 
+    {
+        "Code": 8, 
+        "HttpStatus": 400, 
+        "Name": "BadRequest", 
+        "Description": "Bad request"
+    }, 
+    {
+        "Code": 9, 
+        "Name": "NetworkProtocol", 
+        "Description": "Error in the network protocol"
+    }, 
+    {
+        "Code": 10, 
+        "Name": "SystemCommand", 
+        "Description": "Error while calling a system command"
+    }, 
+    {
+        "Code": 11, 
+        "Name": "Database", 
+        "Description": "Error with the database engine"
+    }, 
+    {
+        "Code": 12, 
+        "HttpStatus": 400, 
+        "Name": "UriSyntax", 
+        "Description": "Badly formatted URI"
+    }, 
+    {
+        "Code": 13, 
+        "HttpStatus": 404, 
+        "Name": "InexistentFile", 
+        "Description": "Inexistent file"
+    }, 
+    {
+        "Code": 14, 
+        "Name": "CannotWriteFile", 
+        "Description": "Cannot write to file"
+    }, 
+    {
+        "Code": 15, 
+        "HttpStatus": 400, 
+        "Name": "BadFileFormat", 
+        "Description": "Bad file format"
+    }, 
+    {
+        "Code": 16, 
+        "Name": "Timeout", 
+        "Description": "Timeout"
+    }, 
+    {
+        "Code": 17, 
+        "HttpStatus": 404, 
+        "Name": "UnknownResource", 
+        "Description": "Unknown resource"
+    }, 
+    {
+        "Code": 18, 
+        "Name": "IncompatibleDatabaseVersion", 
+        "Description": "Incompatible version of the database"
+    }, 
+    {
+        "Code": 19, 
+        "Name": "FullStorage", 
+        "Description": "The file storage is full"
+    }, 
+    {
+        "Code": 20, 
+        "Name": "CorruptedFile", 
+        "Description": "Corrupted file (inconsistent MD5 hash)"
+    }, 
+    {
+        "Code": 21, 
+        "HttpStatus": 404, 
+        "Name": "InexistentTag", 
+        "Description": "Inexistent tag"
+    }, 
+    {
+        "Code": 22, 
+        "Name": "ReadOnly", 
+        "Description": "Cannot modify a read-only data structure"
+    }, 
+    {
+        "Code": 23, 
+        "Name": "IncompatibleImageFormat", 
+        "Description": "Incompatible format of the images"
+    }, 
+    {
+        "Code": 24, 
+        "Name": "IncompatibleImageSize", 
+        "Description": "Incompatible size of the images"
+    }, 
+    {
+        "Code": 25, 
+        "Name": "SharedLibrary", 
+        "Description": "Error while using a shared library (plugin)"
+    }, 
+    {
+        "Code": 26, 
+        "Name": "Plugin", 
+        "Description": "Error encountered inside a plugin"
+    }
+]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/GenerateErrorCodes.py	Tue Aug 25 14:48:06 2015 +0200
@@ -0,0 +1,61 @@
+#!/usr/bin/python
+
+# Orthanc - A Lightweight, RESTful DICOM Store
+# Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+# Department, University Hospital of Liege, Belgium
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# In addition, as a special exception, the copyright holders of this
+# program give permission to link the code of its release with the
+# OpenSSL project's "OpenSSL" library (or with modified versions of it
+# that use the same license as the "OpenSSL" library), and distribute
+# the linked executables. You must obey the GNU General Public License
+# in all respects for all of the code used other than "OpenSSL". If you
+# modify file(s) with this exception, you may extend this exception to
+# your version of the file(s), but you are not obligated to do so. If
+# you do not wish to do so, delete this exception statement from your
+# version. If you delete this exception statement from all source files
+# in the program, then also delete it here.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import json
+import os
+import re
+
+BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+
+
+## 
+## Read all the available error codes
+##
+
+with open(os.path.join(BASE, 'Resources', 'ErrorCodes.json'), 'r') as f:
+    ERRORS = json.loads(f.read())
+
+
+##
+## Generate the "ErrorCode" enumeration in "Core/Enumerations.h"
+##
+
+s = ',\n'.join(map(lambda x: '    ErrorCode_%s = %d' % (x['Name'], int(x['Code'])), ERRORS))
+
+with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'r') as f:
+    a = f.read()
+
+a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
+
+with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'w') as f:
+    f.write(a)
+