PrivMX Endpoint v2.8.0
Loading...
Searching...
No Matches
EventException.hpp
1#ifndef _PRIVMXLIB_ENDPOINT_EVENT_EXT_EXCEPTION_HPP_
2#define _PRIVMXLIB_ENDPOINT_EVENT_EXT_EXCEPTION_HPP_
3
4#include "privmx/endpoint/core/Exception.hpp"
5
6#define DECLARE_SCOPE_ENDPOINT_EXCEPTION(NAME, MSG, SCOPE, CODE, ...) \
7 class NAME : public privmx::endpoint::core::Exception { \
8 public: \
9 static constexpr unsigned int SCOPE_CODE = (CODE); \
10 NAME() : privmx::endpoint::core::Exception(MSG, #NAME, SCOPE, (CODE << 16)) {} \
11 NAME(const std::string& msg, const std::string& name, unsigned int code) \
12 : privmx::endpoint::core::Exception(msg, name, SCOPE, (CODE << 16) | code, std::string()) {} \
13 NAME(const std::string& msg, const std::string& name, unsigned int code, const std::string& description) \
14 : privmx::endpoint::core::Exception(msg, name, SCOPE, (CODE << 16) | code, description) {} \
15 void rethrow() const override; \
16 }; \
17 inline void NAME::rethrow() const { \
18 throw *this; \
19 };
20
21#define DECLARE_ENDPOINT_EXCEPTION(BASE_SCOPED, NAME, MSG, CODE, ...) \
22 class NAME : public BASE_SCOPED { \
23 public: \
24 static constexpr unsigned int FULL_CODE = (BASE_SCOPED::SCOPE_CODE << 16) | (CODE); \
25 NAME() : BASE_SCOPED(MSG, #NAME, CODE) {} \
26 NAME(const std::string& new_of_description) : BASE_SCOPED(MSG, #NAME, CODE, new_of_description) {} \
27 void rethrow() const override; \
28 }; \
29 inline void NAME::rethrow() const { \
30 throw *this; \
31 };
32
33namespace privmx {
34namespace endpoint {
35namespace event {
36// clang-format off
37#define ENDPOINT_EVENT_EXCEPTION_CODE 0x00090000
38
39DECLARE_SCOPE_ENDPOINT_EXCEPTION(EndpointEventException, "Unknown endpoint event exception", "Event", 0x0009)
40
41#define EVENT_EXCEPTIONS(X) \
42 X(ForbiddenChannelNameException, "Forbidden channel name", 0x0002) \
43 X(CannotExtractContextCustomEvent, "Cannot extract ContextCustomEvent", 0x0003) \
44 X(InvalidEncryptedEventDataVersionException, "Invalid version of encrypted event data", 0x0005)
45
46#define PRIVMX_EVENT_DECLARE(NAME, MSG, CODE) DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, NAME, MSG, CODE)
47EVENT_EXCEPTIONS(PRIVMX_EVENT_DECLARE)
48#undef PRIVMX_EVENT_DECLARE
49
50// compile-time guard: codes are collected from the list above, never retyped
51#define PRIVMX_EVENT_CODE(NAME, MSG, CODE) NAME::FULL_CODE,
52static_assert(
53 privmx::endpoint::core::exceptionCodesUnique({EVENT_EXCEPTIONS(PRIVMX_EVENT_CODE)}),
54 "Duplicate exception code in event scope"
55);
56#undef PRIVMX_EVENT_CODE
57#undef EVENT_EXCEPTIONS
58// clang-format on
59} // namespace event
60} // namespace endpoint
61} // namespace privmx
62
63#undef DECLARE_SCOPE_ENDPOINT_EXCEPTION
64#undef DECLARE_ENDPOINT_EXCEPTION
65
66#endif // _PRIVMXLIB_ENDPOINT_EVENT_EXT_EXCEPTION_HPP_