DAVID4 SDK  1.8.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Exceptions.h
1 //=============================================================================
2 // See License in Related Pages
3 //=============================================================================
4 
5 #pragma once
6 
7 #ifndef DAVID_SDK_EXCEPTIONS_H
8 #define DAVID_SDK_EXCEPTIONS_H
9 
10 #include "davidSDK/ErrorCodes.h"
11 #include <string>
12 #include <exception>
13 #include <stdexcept>
14 
15 namespace david {
16 
17 
18 //=============================================================================
19 // Exception
20 //=============================================================================
21 
22 /// Base exception class. All other DAVID exceptions are derived from this class.
23 class Exception : public std::runtime_error
24 {
25 public:
26  /// Constructor.
27  /// @param[in] errorCode Error code.
28  /// @param[in] errorText Textual description of the error.
29  /// @param[in] errorSource Source of the error.
30  Exception(int errorCode, const std::string& errorText, const std::string& errorSource);
31 
32  /// Destructor.
33  virtual ~Exception() throw();
34 
35  /// Returns the error code.
36  /// @return Error code (see david::ErrorCode).
37  int GetErrorCode() const;
38 
39  /// Returns a description for the error.
40  /// @return Error text.
41  std::string GetErrorText() const;
42 
43  /// Returns information about the error source.
44  /// @return Error source.
45  std::string GetErrorSource() const;
46 
47  /// Prints error information to error stream.
48  void PrintError();
49 
50 private:
51  int m_errorCode; ///< Code of the error (see david::ErrorCode).
52  std::string m_errorText; ///< Textual description of the error.
53  std::string m_errorSource; ///< Source of the error.
54 };
55 
56 
57 //=============================================================================
58 // ConnectionException
59 //=============================================================================
60 
61 /// Exception thrown in case of connection errors.
62 /// Associated error codes: All below david::CONNECTION_ERRORS.
64 {
65 public:
66  /// Constructor.
67  /// @param[in] errorCode Error code.
68  /// @param[in] errorText Textual description of the error.
69  /// @param[in] errorSource Source of the error.
70  ConnectionException(int errorCode, const std::string& errorText, const std::string& errorSource);
71 };
72 
73 
74 //=============================================================================
75 // ProtocolException
76 //=============================================================================
77 
78 /// Special connection exception that indicates a problem with the communication protocol.
79 /// Incompatible implementations or wrong/incomplete implementation is likely.
80 /// Associated error codes: All below david::PROTOCOL_ERRORS.
82 {
83 public:
84  /// Constructor.
85  /// @param[in] errorCode Error code.
86  /// @param[in] errorText Textual description of the error.
87  /// @param[in] errorSource Source of the error.
88  ProtocolException(int errorCode, const std::string& errorText, const std::string& errorSource);
89 };
90 
91 
92 //=============================================================================
93 // FunctionException
94 //=============================================================================
95 
96 /// The remote procedure call was excecuted, but caused an error.
97 /// This should be the more 'typical' exception.
98 /// Associated error codes: All above david::CONNECTION_ERRORS.
100 {
101 public:
102  /// Constructor.
103  /// @param[in] errorCode Error code.
104  /// @param[in] errorText Textual description of the error.
105  /// @param[in] errorSource Source of the error.
106  FunctionException(int errorCode, const std::string& errorText, const std::string& errorSource);
107 };
108 
109 
110 //=============================================================================
111 // Global functions
112 //=============================================================================
113 
114 /// Throws an exception.
115 /// @param[in] errorCode Error code.
116 /// @param[in] errorText Textual description of the error.
117 /// @param[in] errorSource Source of the error.
118 void ThrowException(ErrorCode errorCode, const std::string& errorText, const std::string& errorSource);
119 
120 
121 //=============================================================================
122 // ErrorExceptionConverter
123 //=============================================================================
124 
125 /// Easy conversion from error codes to exceptions.
126 /// The error code has to be one of david::ErrorCode.
127 ///
128 /// Example:
129 /// @code
130 /// ErrorExceptionConverter eec;
131 /// eec = AnyFunctionWithErrorCodeAsReturnValue(); // will throw an exception, if return value < 0
132 /// @endcode
134 {
135 public:
136  /// Constructor.
137  /// @param[in] source Optional source information.
138  ErrorExceptionConverter(const std::string& source = "") : m_source(source), m_autoId(0) {}
139 
140  /// Destructor.
142 
143  /// Throws an exception, if 'errorCode' is less then zero.
144  /// An assignment will also increment the automatic Id.
145  /// The automatic Id will will be available in source info of an exception.
146  /// @param[in] errorCode See david::ErrorCode.
147  /// @return Error code.
148  int operator=(int errorCode);
149 
150  /// Set more context information to source.
151  /// The context information will be available in source info of an exception.
152  /// The automatic Id will be reset to zero.
153  /// @param[in] context More context information.
154  /// @return Reference to itself.
155  ErrorExceptionConverter& operator() (const std::string& context);
156 
157  /// Get source information.
158  /// @return Source information.
159  std::string GetSource() const {return m_source;}
160 
161  /// Get context information.
162  /// @return Context information.
163  std::string GetContext() const {return m_context;}
164 
165 private:
166  std::string m_source; ///< Information about error source.
167  std::string m_context; ///< More context information.
168  int m_autoId; ///< Automatic Id.
169 };
170 
171 
172 } // namespace
173 
174 #endif // DAVID_SDK_EXCEPTIONS_H