ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Exceptions.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_EXCEPTIONS_HPP
10 #define RAD_EXCEPTIONS_HPP
11 
12 #include <rad/Helper.hpp>
13 
14 //#include <boost/stacktrace.hpp>
15 #include <boost/filesystem.hpp>
16 
17 #include <stdexcept>
18 #include <string>
19 #include <list>
20 #include <sstream>
21 #include <thread>
22 
23 namespace rad {
24 
25 namespace ErrorMsg {
26 
27 const char* const DB_API_NULL = "DB API returned NULL";
28 const char* const DB_REPLY_ERR = "Received error reply from DB";
29 const char* const DB_CONTEXT_ERR = "DB Context error";
30 const char* const MSG_CONTEXT_ERR = "Error creating msg context";
31 const char* const MSG_SOCKET_ERR = "Error creating socket";
32 const char* const MSG_CONNECT_ERR = "Error connecting socket";
33 const char* const MSG_BIND_ERR = "Error binding socket";
34 const char* const MSG_CONFIG_ERR = "Error configuring socket";
35 const char* const CFG_LOAD = "Loading configuration file";
36 const char* const OPT_INVALID = "Invalid command line option";
37 
38 } // namespace ErrorMsg
39 
44 class Exception : public std::exception /*std::runtime_error*/ {
45 
46 public:
47  Exception(const std::string& msg)
48  : m_message(msg) {}
49 
50  Exception(const std::string& msg, const std::string& info)
51  : m_message(BuildWhatArg(msg, info)) {}
52 
53  Exception(const Exception& e) :
54  std::exception(e) {
55  m_message = e.m_message;
56  }
57 
58  virtual ~Exception() noexcept {}
59 
61  if (&e == this) return *this;
62  std::exception::operator =(e);
63  m_message = e.m_message;
64  return *this;
65  }
66 
67  void AddDiagnostic(const std::string& str) {
68  m_message = m_message + "\n" + str;
69  }
70 
71  virtual const char* what() const throw() {
72  return m_message.c_str(); //std::runtime_error::what();
73 
74  }
75 
76 private:
77  std::string m_message;
78  // boost::stacktrace::stacktrace m_stackTrace;
79 
80  static const std::string BuildWhatArg(const std::string& msg, const std::string& info)
81  {
82  std::stringstream s;
83  s << msg;
84  if (info.size() > 0) {
85  s << " (" << info << ")";
86  }
87  return s.str();
88  }
89 };
90 
94 class RuntimeDbException : public Exception {
95 
96 public:
97  RuntimeDbException(const std::string& msg) : Exception(msg) {}
98  RuntimeDbException(const std::string& msg, const std::string& info) : Exception(msg, info) {}
99  RuntimeDbException(const RuntimeDbException&) = default;
100  virtual ~RuntimeDbException() noexcept {}
101 };
102 
107 
108 public:
109  InvalidOptionException(const std::string& param) : Exception(ErrorMsg::OPT_INVALID, param) {}
111  virtual ~InvalidOptionException() noexcept {}
112 };
113 
119 #define RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, text)\
120  \
121  std::stringstream buf; \
122  buf << rad::GetTimestamp() << " " \
123  << boost::filesystem::path(__FILE__).filename().string() << " " \
124  << __LINE__ << " " << __FUNCTION__ << " " << component << " " \
125  << rad::Helper::GetHostname() << " " << proc_name << " " \
126  << std::hex << std::this_thread::get_id() << " " << getpid() << std::dec << " " \
127  << version << " " << severity << " " << tag << " " << text;
128 
129 #define RAD_THROW(msg, component, proc_name, version, severity, tag) \
130  \
131  RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, "") \
132  throw rad::Exception(msg, buf.str());
133 
134 #define RAD_RETHROW(exception, component, proc_name, version, severity, tag, text) \
135  \
136  RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, text) \
137  exception.AddDiagnostic(buf.str()); \
138  throw;
139 
140 } // namespace rad
141 
142 #endif // RAD_EXCEPTIONS_HPP
143 
InvalidOptionException(const std::string &param)
Definition: Exceptions.hpp:109
virtual ~RuntimeDbException() noexcept
Definition: Exceptions.hpp:100
const char *const DB_API_NULL
Definition: Exceptions.hpp:27
const char *const MSG_SOCKET_ERR
Definition: Exceptions.hpp:31
void AddDiagnostic(const std::string &str)
Definition: Exceptions.hpp:67
Definition: Exceptions.hpp:106
const char *const DB_REPLY_ERR
Definition: Exceptions.hpp:28
const char *const MSG_BIND_ERR
Definition: Exceptions.hpp:33
RuntimeDbException(const std::string &msg)
Definition: Exceptions.hpp:97
Definition: Exceptions.hpp:94
virtual ~InvalidOptionException() noexcept
Definition: Exceptions.hpp:111
optional string msg
Definition: topics.proto:7
RuntimeDbException(const std::string &msg, const std::string &info)
Definition: Exceptions.hpp:98
Exception(const std::string &msg, const std::string &info)
Definition: Exceptions.hpp:50
Exception(const Exception &e)
Definition: Exceptions.hpp:53
Exception & operator=(const Exception &e)
Definition: Exceptions.hpp:60
const char *const MSG_CONFIG_ERR
Definition: Exceptions.hpp:34
const char *const OPT_INVALID
Definition: Exceptions.hpp:36
Definition: Exceptions.hpp:44
const char *const MSG_CONNECT_ERR
Definition: Exceptions.hpp:32
virtual ~Exception() noexcept
Definition: Exceptions.hpp:58
const char *const MSG_CONTEXT_ERR
Definition: Exceptions.hpp:30
const char *const CFG_LOAD
Definition: Exceptions.hpp:35
Exception(const std::string &msg)
Definition: Exceptions.hpp:47
virtual const char * what() const
Definition: Exceptions.hpp:71
const char *const DB_CONTEXT_ERR
Definition: Exceptions.hpp:29