rad  2.0.0
 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 #include <rad/logger.hpp>
14 
15 //#include <boost/stacktrace.hpp>
16 #include <boost/filesystem.hpp>
17 
18 #include <list>
19 #include <sstream>
20 #include <stdexcept>
21 #include <string>
22 #include <thread>
23 
24 namespace rad {
25 
26 namespace errorMsg {
27 
28 const char* const DB_API_NULL = "DB API returned NULL";
29 const char* const DB_REPLY_ERR = "Received error reply from DB";
30 const char* const DB_CONTEXT_ERR = "DB Context error";
31 const char* const MSG_CONTEXT_ERR = "Error creating msg context";
32 const char* const MSG_SOCKET_ERR = "Error creating socket";
33 const char* const MSG_CONNECT_ERR = "Error connecting socket";
34 const char* const MSG_BIND_ERR = "Error binding socket";
35 const char* const MSG_CONFIG_ERR = "Error configuring socket";
36 const char* const CFG_LOAD = "Loading configuration file";
37 const char* const OPT_INVALID = "Invalid command line option";
38 
39 } // namespace errorMsg
40 
45 class Exception : public std::exception /*std::runtime_error*/ {
46  public:
47  explicit Exception(const std::string& msg) : m_message(msg) {}
48 
49  Exception(const std::string& msg, const std::string& info)
50  : m_message(BuildWhatArg(msg, info)) {}
51 
52  Exception(const Exception& e) : std::exception(e) { m_message = e.m_message; }
53 
54  virtual ~Exception() noexcept {}
55 
57  if (&e == this) return *this;
58  std::exception::operator=(e);
59  m_message = e.m_message;
60  return *this;
61  }
62 
63  void AddDiagnostic(const std::string& str) { m_message = m_message + "\n" + str; }
64 
65  virtual const char* what() const noexcept {
66  return m_message.c_str(); // std::runtime_error::what();
67  }
68 
69  private:
70  std::string m_message;
71  // boost::stacktrace::stacktrace m_stackTrace;
72 
73  static const std::string BuildWhatArg(const std::string& msg, const std::string& info) {
74  std::stringstream s;
75  s << msg;
76  if (info.size() > 0) {
77  s << " (" << info << ")";
78  }
79  return s.str();
80  }
81 };
82 
86 class RuntimeDbException : public Exception {
87  public:
88  explicit RuntimeDbException(const std::string& msg) : Exception(msg) {}
89  RuntimeDbException(const std::string& msg, const std::string& info) : Exception(msg, info) {}
90  RuntimeDbException(const RuntimeDbException&) = default;
91  virtual ~RuntimeDbException() noexcept {}
92 };
93 
97 class InvalidOptionException : public Exception {
98  public:
99  explicit InvalidOptionException(const std::string& param)
100  : Exception(errorMsg::OPT_INVALID, param) {}
102  virtual ~InvalidOptionException() noexcept {}
103 };
104 
110 #define RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, text) \
111  \
112  std::stringstream buf; \
113  buf << rad::GetTimestamp() << " " << boost::filesystem::path(__FILE__).filename().string() \
114  << " " << __LINE__ << " " << __FUNCTION__ << " " << component << " " \
115  << rad::Helper::GetHostname() << " " << proc_name << " " << std::hex \
116  << std::this_thread::get_id() << " " << getpid() << std::dec << " " << version << " " \
117  << severity << " " << tag << " " << text;
118 
119 #define RAD_THROW(msg, component, proc_name, version, severity, tag) \
120  \
121  RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, "") \
122  throw rad::Exception(msg, buf.str());
123 
124 #define RAD_RETHROW(exception, component, proc_name, version, severity, tag, text) \
125  \
126  RAD_EXCEPTION_INFO(component, proc_name, version, severity, tag, text) \
127  exception.AddDiagnostic(buf.str()); \
128  throw;
129 
130 } // namespace rad
131 
132 #endif // RAD_EXCEPTIONS_HPP
InvalidOptionException(const std::string &param)
Definition: exceptions.hpp:99
virtual ~RuntimeDbException() noexcept
Definition: exceptions.hpp:91
const char *const MSG_BIND_ERR
Definition: exceptions.hpp:34
void AddDiagnostic(const std::string &str)
Definition: exceptions.hpp:63
virtual const char * what() const noexcept
Definition: exceptions.hpp:65
const char *const MSG_SOCKET_ERR
Definition: exceptions.hpp:32
Definition: exceptions.hpp:97
const char *const DB_REPLY_ERR
Definition: exceptions.hpp:29
const char *const DB_API_NULL
Definition: exceptions.hpp:28
const char *const OPT_INVALID
Definition: exceptions.hpp:37
RuntimeDbException(const std::string &msg)
Definition: exceptions.hpp:88
Definition: exceptions.hpp:86
virtual ~InvalidOptionException() noexcept
Definition: exceptions.hpp:102
const char *const DB_CONTEXT_ERR
Definition: exceptions.hpp:30
RuntimeDbException(const std::string &msg, const std::string &info)
Definition: exceptions.hpp:89
Exception(const std::string &msg, const std::string &info)
Definition: exceptions.hpp:49
Exception(const Exception &e)
Definition: exceptions.hpp:52
Exception & operator=(const Exception &e)
Definition: exceptions.hpp:56
const char *const MSG_CONTEXT_ERR
Definition: exceptions.hpp:31
const char *const CFG_LOAD
Definition: exceptions.hpp:36
Definition: exceptions.hpp:45
const char *const MSG_CONNECT_ERR
Definition: exceptions.hpp:33
virtual ~Exception() noexcept
Definition: exceptions.hpp:54
Exception(const std::string &msg)
Definition: exceptions.hpp:47
const char *const MSG_CONFIG_ERR
Definition: exceptions.hpp:35