PrivMX Endpoint v2.7.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 <string>
6
7namespace privmx {
8namespace endpoint {
9namespace core {
10
11class Exception : public std::exception {
12public:
13 Exception(const std::string& msg = std::string(), const std::string& name = std::string(),
14 const std::string& scope = std::string(), unsigned int code = 0,
15 const std::string& description = std::string())
16 : _msg(msg), _name(name), _scope(scope), _code(code), _description(description) {}
17 virtual const char* what() const noexcept override { return _msg.c_str(); }
18 std::string getName() const noexcept;
19 std::string getScope() const noexcept;
20 unsigned int getCode() const noexcept;
21 std::string getDescription() const noexcept;
22 std::string getFull(bool JSON = false) const noexcept;
23 virtual void rethrow() const;
24
25private:
26 std::string _msg;
27 std::string _name;
28 std::string _scope;
29 unsigned int _code;
30 std::string _description;
31};
32
33inline std::string Exception::getName() const noexcept {
34 return _name;
35}
36
37inline std::string Exception::getScope() const noexcept {
38 return _scope;
39}
40
41inline unsigned int Exception::getCode() const noexcept {
42 return _code;
43}
44
45inline std::string Exception::getDescription() const noexcept {
46 return _description;
47}
48
49inline std::string Exception::getFull(bool JSON) const noexcept {
50 if (JSON) {
51 std::string res = "";
52 res += "{";
53 res += "\"name\" : \"" + _name + "\",";
54 res += "\"scope\" : \"" + _scope + "\",";
55 res += "\"msg\" : \"" + _msg + "\",";
56 res += "\"code\" : " + std::to_string(_code) + ",";
57 res += "\"description\" : \"" + _description + "\"";
58 res += "}";
59 return res;
60 }
61 std::string res = "";
62
63 res += "[" + _scope + "]";
64 res += " " + _name;
65 res += " (code: " + std::to_string(_code);
66 res += ", msg: \"" + _msg + "\")";
67 res += "\n\nDescription: \n" + _description;
68 return res;
69}
70
71inline void Exception::rethrow() const {
72 throw *this;
73}
74
75// used scope codes
76// 0x0000 - Unknown
77// 0x0001 - Core
78// 0x0002 - Connection
79// 0x0003 - Thread
80// 0x0004 - Store
81// 0x0005 - Interface
82// 0x0007 - Inbox
83// 0x0008 - Stream
84// 0x0009 - Event
85// 0x000A - Kvdb
86// Form 0xE000 to 0xEFFF - Internal (PrivmxExtException)
87// Form 0xF000 to 0xFFFF - Server
88//
89
90} // namespace core
91} // namespace endpoint
92} // namespace privmx
93
94#endif // _PRIVMXLIB_ENDPOINT_CORE_EXCEPTION_HPP_