SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Exception.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_Exception_h
7 #define swcdb_core_Exception_h
8 
9 
10 #include "swcdb/core/Compat.h"
11 #include "swcdb/core/Error.h"
12 #include "swcdb/core/String.h"
13 #include "swcdb/core/Logger.h"
14 
15 #include <stdexcept>
16 
17 
18 namespace SWC { namespace Error {
19 
20 
21 class Exception final : public std::exception {
22  public:
23 
24  static Exception make(std::exception_ptr&& eptr,
25  std::string&& msg,
26  Exception* prev = nullptr) noexcept;
27 
28  Exception(
29  int code,
30  std::string&& msg,
31  Exception* prev = nullptr,
32  int line = 0,
33  const char* func = nullptr,
34  const char* file = nullptr,
35  const char* inner_msg = nullptr
36  ) noexcept;
37 
38  Exception(Exception&& other) noexcept;
39 
40  Exception(Exception& other) noexcept;
41 
42  Exception(const Exception& ) = delete;
43 
44  Exception& operator=(const Exception& ) = delete;
45 
46  Exception& operator=(Exception&& ) noexcept = delete;
47 
48  ~Exception() noexcept;
49 
50  constexpr SWC_CAN_INLINE
51  int code() const noexcept {
52  return _code;
53  }
54 
56  virtual const char* what() const noexcept override {
57  return _msg.c_str();
58  }
59 
60  constexpr SWC_CAN_INLINE
61  int line() const noexcept {
62  return _line;
63  }
64 
65  constexpr SWC_CAN_INLINE
66  const char* func() const noexcept {
67  return _func;
68  }
69 
70  constexpr SWC_CAN_INLINE
71  const char* file() const noexcept {
72  return _file;
73  }
74 
76  const char* inner_what() const noexcept {
77  return _inner_msg.c_str();
78  }
79 
80  std::string message() const;
81 
82  void print(std::ostream& out) const;
83 
84  void print_base(std::ostream& out) const;
85 
86  friend std::ostream& operator<<(std::ostream& out, const Exception& e) {
87  e.print(out);
88  return out;
89  }
90 
91  private:
92 
93  int _code;
94  std::string _msg;
95  const int _line;
96  const char* _func;
97  const char* _file;
98  std::string _inner_msg;
99  mutable const Exception* _prev;
100 
101 };
102 
103 
104 }} // namespace SWC::Error
105 
106 
107 
108 
109 // EXCEPTION HELPERS
110 #define SWC_EXCEPTION(_code_, _msg_) \
111  ::SWC::Error::Exception(\
112  _code_, _msg_, nullptr, __LINE__, __PRETTY_FUNCTION__, __FILE__)
113 
114 #define SWC_EXCEPTION2(_code_, _ex_, _msg_) \
115  ::SWC::Error::Exception(\
116  _code_, _msg_, &_ex_, __LINE__, __PRETTY_FUNCTION__, __FILE__)
117 
118 
119 #define SWC_CURRENT_EXCEPTION(_msg_) \
120  ::SWC::Error::Exception::make(std::current_exception(), _msg_)
121 
122 
123 /* preferred:
124 try {
125  --code--
126 } catch (...) {
127  const Error::Exception& e = SWC_CURRENT_EXCEPTION("INFO..");
128  err = e.code();
129 }
130 */
131 
132 
133 // THROW HELPERS
134 #define SWC_THROW(_code_, _msg_) throw SWC_EXCEPTION(_code_, _msg_)
135 
136 #define SWC_THROWF(_code_, _fmt_, ...) \
137  throw SWC_EXCEPTION(_code_, ::SWC::format(_fmt_, __VA_ARGS__))
138 
139 #define SWC_THROW2F(_code_, _ex_, _fmt_, ...) \
140  throw SWC_EXCEPTION2(_code_, _ex_, ::SWC::format(_fmt_, __VA_ARGS__))
141 
142 
143 // LOG HELPERS
144 #define SWC_LOG_CURRENT_EXCEPTION(_s_) \
145  SWC_LOG_OUT(::SWC::LOG_ERROR, \
146  SWC_LOG_OSTREAM << SWC_CURRENT_EXCEPTION(_s_); );
147 
148 
149 // TRY HELPERS
150 #define SWC_TRY(_s_, _code_) \
151  try { _code_ } catch(...) { throw SWC_CURRENT_EXCEPTION(_s_); }
152 
153 #define SWC_TRY_OR_LOG(_s_, _code_) \
154  try { _code_ } catch(...) { SWC_LOG_CURRENT_EXCEPTION(_s_); }
155 
156 
157 // CONDITION HELPERS
158 #define SWC_EXPECT(_e_, _code_) \
159  if (!(_e_)) { \
160  if (_code_ == ::SWC::Error::FAILED_EXPECTATION) \
161  SWC_LOG_FATAL("failed expectation: " #_e_); \
162  SWC_THROW(_code_, "failed expectation: " #_e_); \
163  }
164 
165 #define SWC_ASSERT(_e_) SWC_EXPECT(_e_, ::SWC::Error::FAILED_EXPECTATION)
166 
167 
168 #define SWC_EXPECTF(_e_, _code_, _fmt_, ...) \
169  if (!(_e_)) { \
170  if (_code_ == ::SWC::Error::FAILED_EXPECTATION) \
171  SWC_LOG_FATALF(_fmt_, __VA_ARGS__); \
172  SWC_THROWF(_code_, _fmt_, __VA_ARGS__); \
173  }
174 
175 #define SWC_ASSERTF(_e_, _fmt_, ...) \
176  SWC_EXPECTF(_e_, ::SWC::Error::FAILED_EXPECTATION, _fmt_, __VA_ARGS__)
177 
178 
179 
180 
181 #ifdef SWC_IMPL_SOURCE
182 #include "swcdb/core/Exception.cc"
183 #endif
184 
185 
186 #endif // swcdb_core_Exception_h
Logger.h
SWC::Error::Exception::code
constexpr SWC_CAN_INLINE int code() const noexcept
Definition: Exception.h:51
SWC::Error::Exception::Exception
Exception(const Exception &)=delete
SWC::Error::Exception::_func
const char * _func
Definition: Exception.h:96
Exception.cc
SWC::Thrift::Converter::exception
void exception(int err, const std::string &msg="")
Definition: Converter.h:23
SWC::Error::Exception::operator=
Exception & operator=(const Exception &)=delete
SWC::Error::Exception::print
void print(std::ostream &out) const
Definition: Exception.cc:170
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Error::Exception::_prev
const Exception * _prev
Definition: Exception.h:99
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Error::Exception::file
constexpr SWC_CAN_INLINE const char * file() const noexcept
Definition: Exception.h:71
SWC::Error::Exception::make
static Exception make(std::exception_ptr &&eptr, std::string &&msg, Exception *prev=nullptr) noexcept
Definition: Exception.cc:27
Compat.h
SWC::Error::Exception::func
constexpr SWC_CAN_INLINE const char * func() const noexcept
Definition: Exception.h:66
Error.h
SWC::Error::Exception::operator<<
friend std::ostream & operator<<(std::ostream &out, const Exception &e)
Definition: Exception.h:86
SWC::Error::Exception::line
constexpr SWC_CAN_INLINE int line() const noexcept
Definition: Exception.h:61
SWC::Error::Exception::message
std::string message() const
Definition: Exception.cc:163
SWC::Error::Exception::operator=
Exception & operator=(Exception &&) noexcept=delete
SWC::Error::Exception::_line
const int _line
Definition: Exception.h:95
SWC::Error::Exception::print_base
void print_base(std::ostream &out) const
Definition: Exception.cc:177
SWC::Error::Exception::_msg
std::string _msg
Definition: Exception.h:94
String.h
SWC::Error::Exception::_file
const char * _file
Definition: Exception.h:97
SWC::Error::Exception::what
virtual SWC_CAN_INLINE const char * what() const noexcept override
Definition: Exception.h:56
SWC::Error::Exception::Exception
Exception(int code, std::string &&msg, Exception *prev=nullptr, int line=0, const char *func=nullptr, const char *file=nullptr, const char *inner_msg=nullptr) noexcept
Definition: Exception.cc:115
SWC::Error::Exception::_code
int _code
Definition: Exception.h:93
SWC::Error::Exception::inner_what
SWC_CAN_INLINE const char * inner_what() const noexcept
Definition: Exception.h:76
SWC::Error::Exception
Definition: Exception.h:21
SWC::Error::Exception::_inner_msg
std::string _inner_msg
Definition: Exception.h:98