comparison Core/OrthancException.cpp @ 1575:f8aae45011c9

exceptions now carry HTTP status
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 14:00:57 +0200
parents 818ae9bc493a
children
comparison
equal deleted inserted replaced
1574:0c29ebe80ac9 1575:f8aae45011c9
33 #include "PrecompiledHeaders.h" 33 #include "PrecompiledHeaders.h"
34 #include "OrthancException.h" 34 #include "OrthancException.h"
35 35
36 namespace Orthanc 36 namespace Orthanc
37 { 37 {
38 HttpStatus OrthancException::ConvertToHttpStatus(ErrorCode code)
39 {
40 switch (code)
41 {
42 case ErrorCode_Success:
43 {
44 return HttpStatus_200_Ok;
45 }
46
47 case ErrorCode_InexistentFile:
48 case ErrorCode_InexistentItem:
49 case ErrorCode_InexistentTag:
50 case ErrorCode_UnknownResource:
51 {
52 return HttpStatus_404_NotFound;
53 }
54
55 case ErrorCode_BadFileFormat:
56 case ErrorCode_BadParameterType:
57 case ErrorCode_BadRequest:
58 case ErrorCode_ParameterOutOfRange:
59 case ErrorCode_UriSyntax:
60 {
61 return HttpStatus_400_BadRequest;
62 break;
63 }
64
65 default:
66 {
67 return HttpStatus_500_InternalServerError;
68 }
69 }
70 }
71
38 const char* OrthancException::What() const 72 const char* OrthancException::What() const
39 { 73 {
40 if (error_ == ErrorCode_Custom) 74 if (errorCode_ == ErrorCode_Custom)
41 { 75 {
42 return custom_.c_str(); 76 return custom_.c_str();
43 } 77 }
44 else 78 else
45 { 79 {
46 return GetDescription(error_); 80 return EnumerationToString(errorCode_);
47 } 81 }
48 } 82 }
49 83
50 84
51 const char* OrthancException::GetDescription(ErrorCode error) 85 OrthancException::OrthancException(ErrorCode errorCode) :
86 errorCode_(errorCode),
87 httpStatus_(ConvertToHttpStatus(errorCode))
52 { 88 {
53 switch (error)
54 {
55 case ErrorCode_Success:
56 return "Success";
57
58 case ErrorCode_ParameterOutOfRange:
59 return "Parameter out of range";
60
61 case ErrorCode_NotImplemented:
62 return "Not implemented yet";
63
64 case ErrorCode_InternalError:
65 return "Internal error";
66
67 case ErrorCode_NotEnoughMemory:
68 return "Not enough memory";
69
70 case ErrorCode_UriSyntax:
71 return "Badly formatted URI";
72
73 case ErrorCode_BadParameterType:
74 return "Bad type for a parameter";
75
76 case ErrorCode_InexistentFile:
77 return "Inexistent file";
78
79 case ErrorCode_BadFileFormat:
80 return "Bad file format";
81
82 case ErrorCode_CannotWriteFile:
83 return "Cannot write to file";
84
85 case ErrorCode_Timeout:
86 return "Timeout";
87
88 case ErrorCode_UnknownResource:
89 return "Unknown resource";
90
91 case ErrorCode_BadSequenceOfCalls:
92 return "Bad sequence of calls";
93
94 case ErrorCode_IncompatibleDatabaseVersion:
95 return "Incompatible version of the database";
96
97 case ErrorCode_FullStorage:
98 return "The file storage is full";
99
100 case ErrorCode_InexistentItem:
101 return "Accessing an inexistent item";
102
103 case ErrorCode_BadRequest:
104 return "Bad request";
105
106 case ErrorCode_NetworkProtocol:
107 return "Error in the network protocol";
108
109 case ErrorCode_CorruptedFile:
110 return "Corrupted file (inconsistent MD5 hash)";
111
112 case ErrorCode_InexistentTag:
113 return "Inexistent tag";
114
115 case ErrorCode_ReadOnly:
116 return "Cannot modify a read-only data structure";
117
118 case ErrorCode_IncompatibleImageSize:
119 return "Incompatible size of the images";
120
121 case ErrorCode_IncompatibleImageFormat:
122 return "Incompatible format of the images";
123
124 case ErrorCode_SharedLibrary:
125 return "Error while using a shared library (plugin)";
126
127 case ErrorCode_SystemCommand:
128 return "Error while calling a system command";
129
130 case ErrorCode_Plugin:
131 return "Error encountered inside a plugin";
132
133 case ErrorCode_Database:
134 return "Error with the database engine";
135
136 case ErrorCode_Custom:
137 return "See the attached error message";
138
139 default:
140 return "Unknown error code";
141 }
142 } 89 }
143 } 90 }