DDS  ver. 3.4
ErrorCode.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // error cods collection file
4 //
5 #ifndef ERRORCODE_H
6 #define ERRORCODE_H
7 
8 // API
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
12 // STD
13 #include <sstream>
14 #include <string>
15 
16 namespace MiscCommon
17 {
18 
19  typedef int ERRORCODE;
20  typedef const ERRORCODE ERRORCODE_C;
21 
22  ERRORCODE_C erOK(EXIT_SUCCESS);
23  ERRORCODE_C erTrue(-1);
24  ERRORCODE_C erFalse(-2);
25  ERRORCODE_C erError(EXIT_FAILURE);
27 
28 #define SUCCEEDED(er) (er <= erOK)
29 #define FAILED(er) (!SUCCEEDED(er))
30 
31  // General Error codes
32  ERRORCODE_C BASE_FOR_GENERAL_ERR(50); // <<<----- BASE counter
35 
36  // XML Error codes
37  ERRORCODE_C BASE_FOR_XML_ERR(100); // <<<----- BASE counter
41 
50  inline int errno2str(std::string* _msg)
51  {
52  if (!_msg)
53  return erNULLArg;
54  char* p = strerror(errno);
55  *_msg = p;
56  return errno;
57  }
64  inline std::string errno2str()
65  {
66  char* p = strerror(errno);
67  return std::string(p);
68  }
77  class system_error : public std::exception
78  {
79  public:
80  explicit system_error(const std::string& _ErrorPrefix)
81  {
82  m_errno = errno;
83  const char* const szError = strerror(m_errno);
84  std::stringstream ss;
85  if (!_ErrorPrefix.empty())
86  ss << _ErrorPrefix << ". ";
87  ss << "System error description [" << m_errno << "]: " << szError;
88  m_Msg = ss.str();
89  }
90  virtual ~system_error() throw()
91  {
92  }
93  virtual const char* what() const throw()
94  {
95  return m_Msg.c_str();
96  }
97  int getErrno() const throw()
98  {
99  return m_errno;
100  }
101 
102  private:
103  std::string m_Msg;
104  int m_errno;
105  };
106 }; // namespace MiscCommon
107 
108 #endif
system_error(const std::string &_ErrorPrefix)
Definition: ErrorCode.h:80
ERRORCODE_C erXMLReadConfig(BASE_FOR_XML_ERR+2)
ERRORCODE_C erFalse(-2)
ERRORCODE_C BASE_FOR_GENERAL_ERR(50)
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:77
ERRORCODE_C erXMLNullNode(BASE_FOR_XML_ERR+3)
ERRORCODE_C erOK(EXIT_SUCCESS)
const ERRORCODE ERRORCODE_C
Definition: ErrorCode.h:20
int ERRORCODE
Definition: ErrorCode.h:19
ERRORCODE_C erTrue(-1)
ERRORCODE_C erXMLInit(BASE_FOR_XML_ERR+1)
ERRORCODE_C erNULLArg(BASE_FOR_GENERAL_ERR+1)
ERRORCODE_C erNotImpl(3)
ERRORCODE_C erError(EXIT_FAILURE)
int getErrno() const
Definition: ErrorCode.h:97
int errno2str(std::string *_msg)
Retrieves a string, which represent the last error.
Definition: ErrorCode.h:50
ERRORCODE_C BASE_FOR_XML_ERR(100)
virtual const char * what() const
Definition: ErrorCode.h:93
ERRORCODE_C erFILE_NOT_FOUND(BASE_FOR_GENERAL_ERR+2)
virtual ~system_error()
Definition: ErrorCode.h:90
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21