EpcTools
An event based multi-threaded C++ development framework.
eerror.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __eerror_h_included
19 #define __eerror_h_included
20 
21 #include <exception>
22 
23 #include "estring.h"
24 
27 
29 struct EErrorMapEntry
30 {
31  Long m_nError;
32  cpStr m_pszError;
33 };
35 
39 #define DECLARE_ERR_MAP(__name__) EErrorMapEntry __name__[];
40 #define BGN_ERR_MAP(__name__) EErrorMapEntry __name__[] = {
42 #define ERR_MAP_ENTRY(id, err) {id, err},
44 #define END_ERR_MAP() \
46  { \
47  0, NULL \
48  } \
49  } \
50  ;
51 
53 #define DECLARE_ERROR(__e__) \
54  class __e__ : public EError \
55  { \
56  virtual const cpStr Name() const { return #__e__; } \
57  }
58 #define DECLARE_ERROR_ADVANCED(__e__) \
60  class __e__ : public EError \
61  { \
62  public: \
63  __e__(); \
64  virtual const cpStr Name() const { return #__e__; } \
65  }
66 #define DECLARE_ERROR_ADVANCED2(__e__) \
68  class __e__ : public EError \
69  { \
70  public: \
71  __e__(Int err); \
72  virtual const cpStr Name() const { return #__e__; } \
73  }
74 #define DECLARE_ERROR_ADVANCED3(__e__) \
76  class __e__ : public EError \
77  { \
78  public: \
79  __e__(Int err, cpChar msg); \
80  virtual const cpStr Name() const { return #__e__; } \
81  }
82 #define DECLARE_ERROR_ADVANCED4(__e__) \
84  class __e__ : public EError \
85  { \
86  public: \
87  __e__(cpStr msg); \
88  virtual const cpStr Name() const { return #__e__; } \
89  }
90 
92 class EError : public std::exception
93 {
94 public:
96  enum Severity
97  {
104  };
106  Dword m_dwError;
109 
110 public:
113  {
114  m_eSeverity = Info;
115  m_dwError = 0;
116  }
120  EError(Severity eSeverity, cpStr pszText=NULL)
121  {
122  m_eSeverity = eSeverity;
123  m_dwError = 0;
124  if (pszText)
125  setText(pszText);
126  }
131  EError(Severity eSeverity, Dword err, cpStr pszText=NULL)
132  {
133  m_eSeverity = eSeverity;
134  m_dwError = err;
135  if (pszText)
136  setText(pszText);
137  }
141  EError(Severity eSeverity, const std::string &txt)
142  {
143  m_eSeverity = eSeverity;
144  m_dwError = 0;
145  setText(txt.c_str());
146  }
151  EError(Severity eSeverity, Dword err, const std::string &txt)
152  {
153  m_eSeverity = eSeverity;
154  m_dwError = err;
155  setText(txt.c_str());
156  }
158  EError(const EError &val)
159  {
160  copy(val);
161  }
165  EError &operator=(const EError &val)
166  {
167  return copy(val);
168  }
171  operator cpStr() { return m_str.c_str(); }
175  EError &copy(const EError &val)
176  {
177  m_str.assign(val.m_str);
178  m_dwError = val.m_dwError;
179  m_eSeverity = val.m_eSeverity;
180  return *this;
181  }
182 
189  virtual const cpStr Name() const
190  {
191  return "EError";
192  }
194  Void clear()
195  {
196  clear();
197  }
200  cpStr getText()
201  {
202  return *this;
203  }
206  Void setText(cpStr pszText)
207  {
208  m_str.assign(pszText);
209  }
214  Void setTextf(cpStr pszFormat, ...);
217  Void appendText(cpStr pszText)
218  {
219  m_str.append(pszText);
220  }
226  Void appendTextf(cpStr pszText, ...);
227 
230  Void setSeverity(Severity eSeverity)
231  {
232  m_eSeverity = eSeverity;
233  }
235  Void setSevere()
236  {
237  m_eSeverity = Error;
238  }
240  Void setWarning()
241  {
243  }
245  Void setInfo()
246  {
247  m_eSeverity = Info;
248  }
251  Bool isSevere()
252  {
253  return m_eSeverity == Error;
254  }
257  Bool isWarning()
258  {
259  return m_eSeverity == Warning;
260  }
263  Bool isInfo()
264  {
265  return m_eSeverity == Info;
266  }
269  Bool isError()
270  {
271  return m_eSeverity == Error;
272  }
276  {
277  return m_eSeverity != Info;
278  }
282  {
283  return m_eSeverity;
284  }
288  {
289  return m_pszSeverity[m_eSeverity];
290  }
294  static cpStr getSeverityText(Severity eSeverity)
295  {
296  return m_pszSeverity[eSeverity];
297  }
298 
302  {
303  return m_dwError;
304  }
307  Void setLastOsError(Dword dwError = -1);
310  Void appendLastOsError(Dword dwError = -1);
311 
316  static cpStr getError(Int nError, EErrorMapEntry *pErrors);
317 
320  virtual const char* what() const throw ()
321  {
322  return m_str.c_str();
323  }
324 
325 protected:
327  static cpStr m_pszSeverity[];
329 
330 private:
331  EString m_str;
332 };
333 
334 #endif // #define __eerror_h_included
EError::setTextf
Void setTextf(cpStr pszFormat,...)
Sets the text associated with this error.
Definition: eerror.cpp:29
EError::Severity
Severity
Error severity levels.
Definition: eerror.h:96
EError::EError
EError(Severity eSeverity, Dword err, cpStr pszText=NULL)
Additonal constructor.
Definition: eerror.h:131
EError::EError
EError(Severity eSeverity, const std::string &txt)
Additonal constructor.
Definition: eerror.h:141
EError::m_eSeverity
Severity m_eSeverity
Represents the severity of the error.
Definition: eerror.h:108
EError::isSevere
Bool isSevere()
Returns True if the severity is "Error".
Definition: eerror.h:251
EError::EError
EError(Severity eSeverity, Dword err, const std::string &txt)
Additonal constructor.
Definition: eerror.h:151
EError::getSeverityText
static cpStr getSeverityText(Severity eSeverity)
Returns the text asociated with the specified severity code.
Definition: eerror.h:294
EError::Info
Informational.
Definition: eerror.h:99
EError::isErrorOrWarning
Bool isErrorOrWarning()
Returns True if the severity is "Error" or "Warning".
Definition: eerror.h:275
EError::m_dwError
Dword m_dwError
The unsigned long error code (if appropriate)
Definition: eerror.h:106
EError::clear
Void clear()
Clears the current contents of this error object.
Definition: eerror.h:194
EError::getText
cpStr getText()
Returns the text associated with this error.
Definition: eerror.h:200
EError::isInfo
Bool isInfo()
Returns True if the severity is "Info".
Definition: eerror.h:263
EError::isError
Bool isError()
Returns True if the severity is "Error".
Definition: eerror.h:269
EError::getSeverity
Severity getSeverity()
Returns the current severity setting of this error object.
Definition: eerror.h:281
EError::setText
Void setText(cpStr pszText)
Sets the text associated with this error.
Definition: eerror.h:206
EError::EError
EError(const EError &val)
Copy constructor.
Definition: eerror.h:158
EError::Name
virtual const cpStr Name() const
Returns the name of this object.
Definition: eerror.h:189
EError::setInfo
Void setInfo()
Sets the severity level of this error object to "Info".
Definition: eerror.h:245
EError::getLastOsError
Dword getLastOsError()
Returns the current value of m_dwError.
Definition: eerror.h:301
EError::appendTextf
Void appendTextf(cpStr pszText,...)
appends the specified text to the current error text using a printf style format string and parameter...
Definition: eerror.cpp:38
EError::setSeverity
Void setSeverity(Severity eSeverity)
Sets the severity level of this error object.
Definition: eerror.h:230
EError::setSevere
Void setSevere()
Sets the severity level of this error object to "Error".
Definition: eerror.h:235
EError::isWarning
Bool isWarning()
Returns True if the severity is "Warning".
Definition: eerror.h:257
EError::appendLastOsError
Void appendLastOsError(Dword dwError=-1)
Sets the value of m_dwError and appends the text description of the error to the current error text.
Definition: eerror.cpp:53
EError::appendText
Void appendText(cpStr pszText)
appends the specified text to the current text of this error.
Definition: eerror.h:217
EError::EError
EError(Severity eSeverity, cpStr pszText=NULL)
Additonal constructor.
Definition: eerror.h:120
EError::copy
EError & copy(const EError &val)
Copies the contents of the specified object to this object.
Definition: eerror.h:175
EString
String class.
Definition: estring.h:30
EError::setWarning
Void setWarning()
Sets the severity level of this error object to "Warning".
Definition: eerror.h:240
EError
The base class for exceptions derived from std::exception.
Definition: eerror.h:92
EError::what
virtual const char * what() const
The overloaded definition of the std::exception::what() method.
Definition: eerror.h:320
EError::Warning
Warning.
Definition: eerror.h:101
estring.h
Encapsulates and extends a std::string object.
EError::operator=
EError & operator=(const EError &val)
Assignment operator.
Definition: eerror.h:165
EError::Error
Error.
Definition: eerror.h:103
EError::EError
EError()
Default constructor.
Definition: eerror.h:112
EError::setLastOsError
Void setLastOsError(Dword dwError=-1)
Sets the value of m_dwError.
Definition: eerror.cpp:47
EError::getSeverityText
cpStr getSeverityText()
Returns the text based on the current error object's severity.
Definition: eerror.h:287
EError::getError
static cpStr getError(Int nError, EErrorMapEntry *pErrors)
Returns a description for the specified application error code.
Definition: eerror.cpp:63