Exceptions.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_EXCEPTIONS_H
  21. #define FIX_EXCEPTIONS_H
  22. #include <string>
  23. #include <stdexcept>
  24. #include "Utility.h"
  25. namespace FIX
  26. {
  27. /// Base QuickFIX exception type.
  28. struct Exception : public std::logic_error
  29. {
  30. Exception( const std::string& t, const std::string& d )
  31. : std::logic_error( d.size() ? t + ": " + d : t ),
  32. type( t ), detail( d )
  33. {}
  34. ~Exception() throw() {}
  35. std::string type;
  36. std::string detail;
  37. };
  38. /// DataDictionary not found for BeginString or ApplVerID
  39. struct DataDictionaryNotFound : public Exception
  40. {
  41. DataDictionaryNotFound( const std::string& v, const std::string& what = "" )
  42. : Exception( "Could not find data dictionary", what ),
  43. version( v ) {}
  44. ~DataDictionaryNotFound() throw() {}
  45. std::string version;
  46. };
  47. /// Field not found inside a message
  48. struct FieldNotFound : public Exception
  49. {
  50. FieldNotFound( int f = 0, const std::string& what = "" )
  51. : Exception( "Field not found", what ),
  52. field( f ) {}
  53. int field;
  54. };
  55. /// Unable to convert field into its native format
  56. struct FieldConvertError : public Exception
  57. {
  58. FieldConvertError( const std::string& what = "" )
  59. : Exception( "Could not convert field", what ) {}
  60. };
  61. /// Unable to parse message
  62. struct MessageParseError : public Exception
  63. {
  64. MessageParseError( const std::string& what = "" )
  65. : Exception( "Could not parse message", what ) {}
  66. };
  67. /// Not a recognizable message
  68. struct InvalidMessage : public Exception
  69. {
  70. InvalidMessage( const std::string& what = "" )
  71. : Exception( "Invalid message", what ) {}
  72. };
  73. /// %Application is not configured correctly
  74. struct ConfigError : public Exception
  75. {
  76. ConfigError( const std::string& what = "" )
  77. : Exception( "Configuration failed", what ) {}
  78. };
  79. /// %Application encountered serious error during runtime
  80. struct RuntimeError : public Exception
  81. {
  82. RuntimeError( const std::string& what = "" )
  83. : Exception( "Runtime error", what ) {}
  84. };
  85. /// Tag number does not exist in specification
  86. struct InvalidTagNumber : public Exception
  87. {
  88. InvalidTagNumber( int f = 0, const std::string& what = "" )
  89. : Exception( "Invalid tag number", what ),
  90. field( f ) {}
  91. int field;
  92. };
  93. /// Required field is not in message
  94. struct RequiredTagMissing : public Exception
  95. {
  96. RequiredTagMissing( int f = 0, const std::string& what = "" )
  97. : Exception( "Required tag missing", what ),
  98. field( f ) {}
  99. int field;
  100. };
  101. /// Field does not belong to message
  102. struct TagNotDefinedForMessage : public Exception
  103. {
  104. TagNotDefinedForMessage( int f = 0, const std::string& what = "" )
  105. : Exception( "Tag not defined for this message type", what ),
  106. field( f ) {}
  107. int field;
  108. };
  109. /// Field exists in message without a value
  110. struct NoTagValue : public Exception
  111. {
  112. NoTagValue( int f = 0, const std::string& what = "" )
  113. : Exception( "Tag specified without a value", what ),
  114. field( f ) {}
  115. int field;
  116. };
  117. /// Field has a value that is out of range
  118. struct IncorrectTagValue : public Exception
  119. {
  120. IncorrectTagValue( int f = 0, const std::string& what = "" )
  121. : Exception( "Value is incorrect (out of range) for this tag", what ),
  122. field( f ) {}
  123. int field;
  124. };
  125. /// Field has a badly formatted value
  126. struct IncorrectDataFormat : public Exception
  127. {
  128. IncorrectDataFormat( int f = 0, const std::string& what = "" )
  129. : Exception( "Incorrect data format for value", what ),
  130. field( f ) {}
  131. int field;
  132. };
  133. /// Message is not structured correctly
  134. struct IncorrectMessageStructure : public Exception
  135. {
  136. IncorrectMessageStructure( const std::string& what = "" )
  137. : Exception( "Incorrect message structure", what ) {}
  138. };
  139. /// Field shows up twice in the message
  140. struct DuplicateFieldNumber : public Exception
  141. {
  142. DuplicateFieldNumber( const std::string& what = "" )
  143. : Exception( "Duplicate field number", what ) {}
  144. };
  145. /// Not a known message type
  146. struct InvalidMessageType : public Exception
  147. {
  148. InvalidMessageType( const std::string& what = "" )
  149. : Exception( "Invalid Message Type", what ) {}
  150. };
  151. /// Message type not supported by application
  152. struct UnsupportedMessageType : public Exception
  153. {
  154. UnsupportedMessageType( const std::string& what = "" )
  155. : Exception( "Unsupported Message Type", what ) {}
  156. };
  157. /// Version of %FIX is not supported
  158. struct UnsupportedVersion : public Exception
  159. {
  160. UnsupportedVersion( const std::string& what = "" )
  161. : Exception( "Unsupported Version", what ) {}
  162. };
  163. /// Tag is not in the correct order
  164. struct TagOutOfOrder : public Exception
  165. {
  166. TagOutOfOrder( int f = 0, const std::string& what = "" )
  167. : Exception( "Tag specified out of required order", what ),
  168. field( f ) {}
  169. int field;
  170. };
  171. /// Repeated tag not part of repeating group
  172. struct RepeatedTag : public Exception
  173. {
  174. RepeatedTag( int f = 0, const std::string& what = "" )
  175. : Exception( "Repeated tag not part of repeating group", what ),
  176. field( f ) {}
  177. int field;
  178. };
  179. /// Repeated group count not equal to actual count
  180. struct RepeatingGroupCountMismatch : public Exception
  181. {
  182. RepeatingGroupCountMismatch( int f = 0, const std::string& what = "" )
  183. : Exception( "Repeating group count mismatch", what ),
  184. field( f ) {}
  185. int field;
  186. };
  187. /// Indicates user does not want to send a message
  188. struct DoNotSend : public Exception
  189. {
  190. DoNotSend( const std::string& what = "" )
  191. : Exception( "Do Not Send Message", what ) {}
  192. };
  193. /// User wants to reject permission to logon
  194. struct RejectLogon : public Exception
  195. {
  196. RejectLogon( const std::string& what = "" )
  197. : Exception( "Rejected Logon Attempt", what ) {}
  198. };
  199. /// Session cannot be found for specified action
  200. struct SessionNotFound : public Exception
  201. {
  202. SessionNotFound( const std::string& what = "" )
  203. : Exception( "Session Not Found", what ) {}
  204. };
  205. /// IO Error
  206. struct IOException : public Exception
  207. {
  208. IOException( const std::string& what = "" )
  209. : Exception( "IO Error", what ) {}
  210. };
  211. /// Socket Error
  212. struct SocketException : public Exception
  213. {
  214. SocketException()
  215. : Exception( "Socket Error", errorToWhat() ) {}
  216. SocketException( const std::string& what )
  217. : Exception( "Socket Error", what ) {}
  218. std::string errorToWhat()
  219. {
  220. #ifdef _MSC_VER
  221. error = WSAGetLastError();
  222. char buffer[2048];
  223. FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
  224. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  225. buffer, 2048, NULL );
  226. return buffer;
  227. #else
  228. error = errno;
  229. return strerror( error );
  230. #endif
  231. }
  232. int error;
  233. };
  234. /// Socket send operation failed
  235. struct SocketSendFailed : public SocketException
  236. {
  237. SocketSendFailed() {}
  238. SocketSendFailed( const std::string& what )
  239. : SocketException( what ) {}
  240. };
  241. /// Socket recv operation failed
  242. struct SocketRecvFailed : public SocketException
  243. {
  244. SocketRecvFailed( int size )
  245. : SocketException( size == 0 ? "Connection reset by peer." : size < 0 ? errorToWhat() : "Success." ) {}
  246. SocketRecvFailed( const std::string& what )
  247. : SocketException( what ) {}
  248. };
  249. /// Socket close operation failed
  250. struct SocketCloseFailed : public SocketException
  251. {
  252. SocketCloseFailed() {}
  253. SocketCloseFailed( const std::string& what )
  254. : SocketException( what ) {}
  255. };
  256. /*! @} */
  257. }
  258. #endif //FIX_EXCEPTIONS_H