PrivMX Endpoint v2.8.0
Loading...
Searching...
No Matches
Exception.hpp
1#ifndef _PRIVMXLIB_ENDPOINT_CORE_EXCEPTION_HPP_
2#define _PRIVMXLIB_ENDPOINT_CORE_EXCEPTION_HPP_
3
4#include <exception>
5#include <initializer_list>
6#include <memory>
7#include <string>
8
9namespace privmx {
10namespace endpoint {
11namespace core {
12
13constexpr bool exceptionCodesUnique(std::initializer_list<unsigned int> codes) {
14 for (auto i = codes.begin(); i != codes.end(); ++i) {
15 for (auto j = i + 1; j != codes.end(); ++j) {
16 if (*i == *j) {
17 return false;
18 }
19 }
20 }
21 return true;
22}
23
24class Exception : public std::exception {
25public:
26 Exception(
27 const std::string& msg = std::string(),
28 const std::string& name = std::string(),
29 const std::string& scope = std::string(),
30 unsigned int code = 0,
31 const std::string& description = std::string()
32 )
33 : _msg(msg), _name(name), _scope(scope), _code(code), _description(description) {
34 _what = _msg;
35 if (!_description.empty()) {
36 _what += " | " + _description;
37 }
38 }
39 virtual const char* what() const noexcept override { return _what.c_str(); }
40 std::string getName() const noexcept;
41 std::string getScope() const noexcept;
42 unsigned int getCode() const noexcept;
43 std::string getDescription() const noexcept;
44 std::string getFull(bool JSON = false) const noexcept;
45 virtual void rethrow() const;
46
47 void setCause(const Exception& cause);
48 std::shared_ptr<Exception> getCause() const noexcept { return _cause; }
49
50private:
51 std::string _msg;
52 std::string _name;
53 std::string _scope;
54 unsigned int _code;
55 std::string _description;
56 std::string _what;
57 std::shared_ptr<Exception> _cause;
58};
59
60inline std::string Exception::getName() const noexcept {
61 return _name;
62}
63
64inline std::string Exception::getScope() const noexcept {
65 return _scope;
66}
67
68inline unsigned int Exception::getCode() const noexcept {
69 return _code;
70}
71
72inline std::string Exception::getDescription() const noexcept {
73 return _description;
74}
75
76inline std::string Exception::getFull(bool JSON) const noexcept {
77 if (JSON) {
78 std::string res = "";
79 res += "{";
80 res += "\"name\" : \"" + _name + "\",";
81 res += "\"scope\" : \"" + _scope + "\",";
82 res += "\"msg\" : \"" + _msg + "\",";
83 res += "\"code\" : " + std::to_string(_code) + ",";
84 res += "\"description\" : \"" + _description + "\",";
85 res += "\"cause\" : " + (_cause ? _cause->getFull(true) : std::string("null"));
86 res += "}";
87 return res;
88 }
89 std::string res = "";
90
91 res += "[" + _scope + "]";
92 res += " " + _name;
93 res += " (code: " + std::to_string(_code);
94 res += ", msg: \"" + _msg + "\")";
95 res += "\n\nDescription: \n" + _description;
96 if (_cause) {
97 res += "\n\nCaused by:\n" + _cause->getFull(false);
98 }
99 return res;
100}
101
102inline void Exception::setCause(const Exception& cause) {
103 _cause = std::make_shared<Exception>(cause);
104 _what += " | caused by: ";
105 _what += _cause->what();
106}
107
108inline void Exception::rethrow() const {
109 throw *this;
110}
111
112// used scope codes
113// 0x0000 - Unknown
114// 0x0001 - Core
115// 0x0002 - Connection
116// 0x0003 - Thread
117// 0x0004 - Store
118// 0x0005 - Interface
119// 0x0007 - Inbox
120// 0x0008 - Stream
121// 0x0009 - Event
122// 0x000A - Kvdb
123// Form 0xE000 to 0xEFFF - Internal (PrivmxExtException)
124// Form 0xF000 to 0xFFFF - Server
125//
126
127} // namespace core
128} // namespace endpoint
129} // namespace privmx
130
131#endif // _PRIVMXLIB_ENDPOINT_CORE_EXCEPTION_HPP_
Definition Exception.hpp:24