16#ifndef OPENVPN_COMMON_EXCEPTION_H
17#define OPENVPN_COMMON_EXCEPTION_H
26#ifdef OPENVPN_DEBUG_EXCEPTION
28#define OPENVPN_FILE_LINE "/" __FILE__ ":" OPENVPN_STRINGIZE(__LINE__)
30#define OPENVPN_FILE_LINE
47 const char *
what() const noexcept
override
51 const std::string &
err() const noexcept
64 const std::string head = label +
": ";
65 if (
err_.starts_with(head))
74#define OPENVPN_SIMPLE_EXCEPTION(C) \
75 class C : public std::exception \
78 const char *what() const noexcept override \
80 return #C OPENVPN_FILE_LINE; \
85#define OPENVPN_SIMPLE_EXCEPTION_INHERIT(B, C) \
89 C() : B(#C OPENVPN_FILE_LINE) \
92 const char *what() const noexcept override \
94 return #C OPENVPN_FILE_LINE; \
99#define OPENVPN_EXCEPTION(C) \
100 class C : public openvpn::Exception \
103 C() : openvpn::Exception(#C OPENVPN_FILE_LINE) \
106 C(const std::string &err) : openvpn::Exception(#C OPENVPN_FILE_LINE ": " + err) \
112#define OPENVPN_EXCEPTION_WITH_CODE(C, DEFAULT_CODE, ...) \
113 enum C##_##code : unsigned int{__VA_ARGS__}; \
114 class C : public openvpn::Exception \
117 C() : openvpn::Exception(#C OPENVPN_FILE_LINE) \
119 add_label(#DEFAULT_CODE); \
121 C(const std::string &err) : openvpn::Exception(#C OPENVPN_FILE_LINE ": " + err) \
123 add_label(#DEFAULT_CODE); \
125 option_error(C##_##code code, const std::string &err) \
126 : openvpn::Exception(#C OPENVPN_FILE_LINE ": " + err) \
128 add_label(code2string(code)); \
130 static std::string code2string(C##_##code code); \
133#define OPENVPN_UNTAGGED_EXCEPTION(C) \
134 class C : public openvpn::Exception \
137 C(const std::string &err) : openvpn::Exception(err) \
143#define OPENVPN_EXCEPTION_INHERIT(B, C) \
147 C() : B(#C OPENVPN_FILE_LINE) \
150 C(const std::string &err) : B(#C OPENVPN_FILE_LINE ": " + err) \
157#define OPENVPN_UNTAGGED_EXCEPTION_INHERIT(B, C) \
165#define OPENVPN_THROW_EXCEPTION(stuff) \
168 std::ostringstream _ovpn_exc; \
169 _ovpn_exc << stuff; \
170 throw openvpn::Exception(_ovpn_exc.str()); \
174#define OPENVPN_THROW(exc, stuff) \
177 std::ostringstream _ovpn_exc; \
178 _ovpn_exc << stuff; \
179 throw exc(_ovpn_exc.str()); \
182#define OPENVPN_THROW_ARG1(exc, arg, stuff) \
185 std::ostringstream _ovpn_exc; \
186 _ovpn_exc << stuff; \
187 throw exc(arg, _ovpn_exc.str()); \
Exception(const std::string &err) noexcept
const std::string & err() const noexcept
const char * what() const noexcept override
virtual ~Exception() noexcept=default
Exception(std::string &&err) noexcept
void remove_label(const std::string &label)
void add_label(const std::string &label)
void throw_ref(const std::exception &e)