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