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  public: \
57  __e__() { setText(#__e__); } \
58  virtual const cpStr Name() const { return #__e__; } \
59  }
60 #define DECLARE_ERROR_ADVANCED(__e__) \
62  class __e__ : public EError \
63  { \
64  public: \
65  __e__(); \
66  virtual const cpStr Name() const { return #__e__; } \
67  }
68 #define DECLARE_ERROR_ADVANCED2(__e__) \
70  class __e__ : public EError \
71  { \
72  public: \
73  __e__(Int err); \
74  virtual const cpStr Name() const { return #__e__; } \
75  }
76 #define DECLARE_ERROR_ADVANCED3(__e__) \
78  class __e__ : public EError \
79  { \
80  public: \
81  __e__(Int err, cpChar msg); \
82  virtual const cpStr Name() const { return #__e__; } \
83  }
84 #define DECLARE_ERROR_ADVANCED4(__e__) \
86  class __e__ : public EError \
87  { \
88  public: \
89  __e__(cpStr msg); \
90  virtual const cpStr Name() const { return #__e__; } \
91  }
92 
94 class EError : public std::exception
95 {
96 public:
98  enum Severity
99  {
105  Error
106  };
108  Dword m_dwError;
111 
112 public:
115  {
116  m_eSeverity = Info;
117  m_dwError = 0;
118  }
122  EError(Severity eSeverity, cpStr pszText=NULL)
123  {
124  m_eSeverity = eSeverity;
125  m_dwError = 0;
126  if (pszText)
127  setText(pszText);
128  }
133  EError(Severity eSeverity, Dword err, cpStr pszText=NULL)
134  {
135  m_eSeverity = eSeverity;
136  m_dwError = err;
137  if (pszText)
138  setText(pszText);
139  }
143  EError(Severity eSeverity, const std::string &txt)
144  {
145  m_eSeverity = eSeverity;
146  m_dwError = 0;
147  setText(txt.c_str());
148  }
153  EError(Severity eSeverity, Dword err, const std::string &txt)
154  {
155  m_eSeverity = eSeverity;
156  m_dwError = err;
157  setText(txt.c_str());
158  }
160  EError(const EError &val)
161  {
162  copy(val);
163  }
167  EError &operator=(const EError &val)
168  {
169  return copy(val);
170  }
173  operator cpStr() { return m_str.c_str(); }
177  EError &copy(const EError &val)
178  {
179  m_str.assign(val.m_str);
180  m_dwError = val.m_dwError;
181  m_eSeverity = val.m_eSeverity;
182  return *this;
183  }
184 
191  virtual const cpStr Name() const
192  {
193  return "EError";
194  }
196  Void clear()
197  {
198  clear();
199  }
202  cpStr getText()
203  {
204  return *this;
205  }
208  Void setText(cpStr pszText)
209  {
210  m_str.assign(pszText);
211  }
216  Void setTextf(cpStr pszFormat, ...);
219  Void appendText(cpStr pszText)
220  {
221  m_str.append(pszText);
222  }
228  Void appendTextf(cpStr pszText, ...);
229 
232  Void setSeverity(Severity eSeverity)
233  {
234  m_eSeverity = eSeverity;
235  }
237  Void setSevere()
238  {
239  m_eSeverity = Error;
240  }
242  Void setWarning()
243  {
244  m_eSeverity = Warning;
245  }
247  Void setInfo()
248  {
249  m_eSeverity = Info;
250  }
253  Bool isSevere()
254  {
255  return m_eSeverity == Error;
256  }
259  Bool isWarning()
260  {
261  return m_eSeverity == Warning;
262  }
265  Bool isInfo()
266  {
267  return m_eSeverity == Info;
268  }
271  Bool isError()
272  {
273  return m_eSeverity == Error;
274  }
278  {
279  return m_eSeverity != Info;
280  }
284  {
285  return m_eSeverity;
286  }
290  {
291  return m_pszSeverity[m_eSeverity];
292  }
296  static cpStr getSeverityText(Severity eSeverity)
297  {
298  return m_pszSeverity[eSeverity];
299  }
300 
304  {
305  return m_dwError;
306  }
309  Void setLastOsError(Dword dwError = -1);
312  Void appendLastOsError(Dword dwError = -1);
313 
318  static cpStr getError(Int nError, EErrorMapEntry *pErrors);
319 
322  virtual const char* what() const throw ()
323  {
324  return m_str.c_str();
325  }
326 
327 protected:
329  static cpStr m_pszSeverity[];
331 
332 private:
333  EString m_str;
334 };
335 
336 #endif // #define __eerror_h_included
static cpStr getSeverityText(Severity eSeverity)
Returns the text asociated with the specified severity code.
Definition: eerror.h:296
Void setWarning()
Sets the severity level of this error object to "Warning".
Definition: eerror.h:242
Bool isSevere()
Returns True if the severity is "Error".
Definition: eerror.h:253
Encapsulates and extends a std::string object.
Bool isErrorOrWarning()
Returns True if the severity is "Error" or "Warning".
Definition: eerror.h:277
Bool isInfo()
Returns True if the severity is "Info".
Definition: eerror.h:265
Void setSevere()
Sets the severity level of this error object to "Error".
Definition: eerror.h:237
Warning.
Definition: eerror.h:103
Severity
Error severity levels.
Definition: eerror.h:98
EError & copy(const EError &val)
Copies the contents of the specified object to this object.
Definition: eerror.h:177
Void setText(cpStr pszText)
Sets the text associated with this error.
Definition: eerror.h:208
Void setInfo()
Sets the severity level of this error object to "Info".
Definition: eerror.h:247
Severity m_eSeverity
Represents the severity of the error.
Definition: eerror.h:110
cpStr getSeverityText()
Returns the text based on the current error object&#39;s severity.
Definition: eerror.h:289
Bool isWarning()
Returns True if the severity is "Warning".
Definition: eerror.h:259
Void setSeverity(Severity eSeverity)
Sets the severity level of this error object.
Definition: eerror.h:232
EError(Severity eSeverity, cpStr pszText=NULL)
Additonal constructor.
Definition: eerror.h:122
EError()
Default constructor.
Definition: eerror.h:114
EError(Severity eSeverity, Dword err, cpStr pszText=NULL)
Additonal constructor.
Definition: eerror.h:133
Severity getSeverity()
Returns the current severity setting of this error object.
Definition: eerror.h:283
The base class for exceptions derived from std::exception.
Definition: eerror.h:94
EError(Severity eSeverity, const std::string &txt)
Additonal constructor.
Definition: eerror.h:143
virtual const char * what() const
The overloaded definition of the std::exception::what() method.
Definition: eerror.h:322
EError & operator=(const EError &val)
Assignment operator.
Definition: eerror.h:167
Informational.
Definition: eerror.h:101
EError(Severity eSeverity, Dword err, const std::string &txt)
Additonal constructor.
Definition: eerror.h:153
Bool isError()
Returns True if the severity is "Error".
Definition: eerror.h:271
Dword m_dwError
The unsigned long error code (if appropriate)
Definition: eerror.h:108
Void appendText(cpStr pszText)
appends the specified text to the current text of this error.
Definition: eerror.h:219
Void clear()
Clears the current contents of this error object.
Definition: eerror.h:196
virtual const cpStr Name() const
Returns the name of this object.
Definition: eerror.h:191
cpStr getText()
Returns the text associated with this error.
Definition: eerror.h:202
String class.
Definition: estring.h:31
Dword getLastOsError()
Returns the current value of m_dwError.
Definition: eerror.h:303
EError(const EError &val)
Copy constructor.
Definition: eerror.h:160