Mercurial > hg > orthanc
changeset 1577:7aac0cddd42e
generation of the error codes from a Python script
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 25 Aug 2015 15:10:45 +0200 |
parents | de54c19fc44d |
children | 09715095fc53 |
files | Core/Enumerations.cpp Core/Enumerations.h Core/OrthancException.h Resources/GenerateErrorCodes.py |
diffstat | 4 files changed, 109 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Enumerations.cpp Tue Aug 25 14:48:06 2015 +0200 +++ b/Core/Enumerations.cpp Tue Aug 25 15:10:45 2015 +0200 @@ -44,12 +44,12 @@ { switch (error) { + case ErrorCode_Custom: + return "Custom error, see the attached error message"; + case ErrorCode_Success: return "Success"; - case ErrorCode_Custom: - return "Custom error, see the attached error message"; - case ErrorCode_InternalError: return "Internal error"; @@ -68,18 +68,33 @@ case ErrorCode_BadSequenceOfCalls: return "Bad sequence of calls"; + case ErrorCode_InexistentItem: + return "Accessing an inexistent item"; + + case ErrorCode_BadRequest: + return "Bad request"; + + case ErrorCode_NetworkProtocol: + return "Error in the network protocol"; + + case ErrorCode_SystemCommand: + return "Error while calling a system command"; + + case ErrorCode_Database: + return "Error with the database engine"; + case ErrorCode_UriSyntax: return "Badly formatted URI"; case ErrorCode_InexistentFile: return "Inexistent file"; + case ErrorCode_CannotWriteFile: + return "Cannot write to file"; + case ErrorCode_BadFileFormat: return "Bad file format"; - case ErrorCode_CannotWriteFile: - return "Cannot write to file"; - case ErrorCode_Timeout: return "Timeout"; @@ -92,15 +107,6 @@ case ErrorCode_FullStorage: return "The file storage is full"; - case ErrorCode_InexistentItem: - return "Accessing an inexistent item"; - - case ErrorCode_BadRequest: - return "Bad request"; - - case ErrorCode_NetworkProtocol: - return "Error in the network protocol"; - case ErrorCode_CorruptedFile: return "Corrupted file (inconsistent MD5 hash)"; @@ -110,24 +116,18 @@ case ErrorCode_ReadOnly: return "Cannot modify a read-only data structure"; + case ErrorCode_IncompatibleImageFormat: + return "Incompatible format of the images"; + case ErrorCode_IncompatibleImageSize: return "Incompatible size of the images"; - case ErrorCode_IncompatibleImageFormat: - return "Incompatible format of the images"; - case ErrorCode_SharedLibrary: return "Error while using a shared library (plugin)"; - case ErrorCode_SystemCommand: - return "Error while calling a system command"; - case ErrorCode_Plugin: return "Error encountered inside a plugin"; - case ErrorCode_Database: - return "Error with the database engine"; - default: return "Unknown error code"; } @@ -926,32 +926,37 @@ switch (error) { case ErrorCode_Success: - { return HttpStatus_200_Ok; - } + + case ErrorCode_ParameterOutOfRange: + return HttpStatus_400_BadRequest; + + case ErrorCode_BadParameterType: + return HttpStatus_400_BadRequest; + + case ErrorCode_InexistentItem: + return HttpStatus_404_NotFound; + + case ErrorCode_BadRequest: + return HttpStatus_400_BadRequest; + + case ErrorCode_UriSyntax: + return HttpStatus_400_BadRequest; 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; - } + + case ErrorCode_UnknownResource: + return HttpStatus_404_NotFound; + + case ErrorCode_InexistentTag: + return HttpStatus_404_NotFound; default: - { return HttpStatus_500_InternalServerError; - } } } }
--- a/Core/Enumerations.h Tue Aug 25 14:48:06 2015 +0200 +++ b/Core/Enumerations.h Tue Aug 25 15:10:45 2015 +0200 @@ -43,37 +43,34 @@ enum ErrorCode { - // Generic error codes - ErrorCode_Success, - ErrorCode_Custom, - ErrorCode_InternalError, - ErrorCode_NotImplemented, - ErrorCode_ParameterOutOfRange, - ErrorCode_NotEnoughMemory, - ErrorCode_BadParameterType, - ErrorCode_BadSequenceOfCalls, - ErrorCode_InexistentItem, - ErrorCode_BadRequest, - ErrorCode_NetworkProtocol, - ErrorCode_SystemCommand, - ErrorCode_Database, - - // Specific error codes - ErrorCode_UriSyntax, - ErrorCode_InexistentFile, - ErrorCode_CannotWriteFile, - ErrorCode_BadFileFormat, - ErrorCode_Timeout, - ErrorCode_UnknownResource, - ErrorCode_IncompatibleDatabaseVersion, - ErrorCode_FullStorage, - ErrorCode_CorruptedFile, - ErrorCode_InexistentTag, - ErrorCode_ReadOnly, - ErrorCode_IncompatibleImageFormat, - ErrorCode_IncompatibleImageSize, - ErrorCode_SharedLibrary, - ErrorCode_Plugin + ErrorCode_Custom = -1, + ErrorCode_Success = 0, + ErrorCode_InternalError = 1, + ErrorCode_NotImplemented = 2, + ErrorCode_ParameterOutOfRange = 3, + ErrorCode_NotEnoughMemory = 4, + ErrorCode_BadParameterType = 5, + ErrorCode_BadSequenceOfCalls = 6, + ErrorCode_InexistentItem = 7, + ErrorCode_BadRequest = 8, + ErrorCode_NetworkProtocol = 9, + ErrorCode_SystemCommand = 10, + ErrorCode_Database = 11, + ErrorCode_UriSyntax = 12, + ErrorCode_InexistentFile = 13, + ErrorCode_CannotWriteFile = 14, + ErrorCode_BadFileFormat = 15, + ErrorCode_Timeout = 16, + ErrorCode_UnknownResource = 17, + ErrorCode_IncompatibleDatabaseVersion = 18, + ErrorCode_FullStorage = 19, + ErrorCode_CorruptedFile = 20, + ErrorCode_InexistentTag = 21, + ErrorCode_ReadOnly = 22, + ErrorCode_IncompatibleImageFormat = 23, + ErrorCode_IncompatibleImageSize = 24, + ErrorCode_SharedLibrary = 25, + ErrorCode_Plugin = 26 }; enum LogLevel
--- a/Core/OrthancException.h Tue Aug 25 14:48:06 2015 +0200 +++ b/Core/OrthancException.h Tue Aug 25 15:10:45 2015 +0200 @@ -44,16 +44,7 @@ HttpStatus httpStatus_; std::string custom_; - static HttpStatus ConvertToHttpStatus(ErrorCode code); - public: - OrthancException(const char* custom) : - errorCode_(ErrorCode_Custom), - httpStatus_(HttpStatus_500_InternalServerError), - custom_(custom) - { - } - OrthancException(const std::string& custom) : errorCode_(ErrorCode_Custom), httpStatus_(HttpStatus_500_InternalServerError),
--- a/Resources/GenerateErrorCodes.py Tue Aug 25 14:48:06 2015 +0200 +++ b/Resources/GenerateErrorCodes.py Tue Aug 25 15:10:45 2015 +0200 @@ -37,25 +37,58 @@ BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + ## -## Read all the available error codes +## Read all the available error codes and HTTP status ## with open(os.path.join(BASE, 'Resources', 'ErrorCodes.json'), 'r') as f: ERRORS = json.loads(f.read()) +with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'r') as f: + a = f.read() + +HTTP = {} +for i in re.findall('(HttpStatus_([0-9]+)_\w+)', a): + HTTP[int(i[1])] = i[0] + + ## ## 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() +s = ',\n'.join(map(lambda x: ' ErrorCode_%s = %d' % (x['Name'], int(x['Code'])), ERRORS)) 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) + + +## +## Generate the "EnumerationToString(ErrorCode)" and +## "ConvertErrorCodeToHttpStatus(ErrorCode)" functions in +## "Core/Enumerations.cpp" +## + +with open(os.path.join(BASE, 'Core', 'Enumerations.cpp'), 'r') as f: + a = f.read() + +s = '\n\n'.join(map(lambda x: ' case ErrorCode_%s:\n return "%s";' % (x['Name'], x['Description']), ERRORS)) +a = re.sub('(EnumerationToString\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)', + r'\1\n%s\2' % s, a, re.DOTALL) + +def GetHttpStatus(x): + s = HTTP[x['HttpStatus']] + return ' case ErrorCode_%s:\n return %s;' % (x['Name'], s) + +s = '\n\n'.join(map(GetHttpStatus, filter(lambda x: 'HttpStatus' in x, ERRORS))) +a = re.sub('(ConvertErrorCodeToHttpStatus\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)', + r'\1\n%s\2' % s, a, re.DOTALL) + +with open(os.path.join(BASE, 'Core', 'Enumerations.cpp'), 'w') as f: + f.write(a)