sdbus-c++ 2.1.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Loading...
Searching...
No Matches
Error.h
Go to the documentation of this file.
1
27#ifndef SDBUS_CXX_ERROR_H_
28#define SDBUS_CXX_ERROR_H_
29
30#include <errno.h>
31#include <stdexcept>
32#include <string>
33
34namespace sdbus {
35
36 /********************************************/
42 class Error
43 : public std::runtime_error
44 {
45 public:
46 // Strong type representing the D-Bus error name
47 class Name : public std::string
48 {
49 public:
50 Name() = default;
51 explicit Name(std::string value)
52 : std::string(std::move(value))
53 {}
54 explicit Name(const char* value)
55 : std::string(value)
56 {}
57
58 using std::string::operator=;
59 };
60
61 explicit Error(Name name, const char* message = nullptr)
62 : Error(std::move(name), std::string(message ? message : ""))
63 {
64 }
65
66 Error(Name name, std::string message)
67 : std::runtime_error("[" + name + "] " + message)
68 , name_(std::move(name))
69 , message_(std::move(message))
70 {
71 }
72
73 [[nodiscard]] const Name& getName() const
74 {
75 return name_;
76 }
77
78 [[nodiscard]] const std::string& getMessage() const
79 {
80 return message_;
81 }
82
83 [[nodiscard]] bool isValid() const
84 {
85 return !getName().empty();
86 }
87
88 private:
89 Name name_;
90 std::string message_;
91 };
92
93 Error createError(int errNo, std::string customMsg);
94
95 inline const Error::Name SDBUSCPP_ERROR_NAME{"org.sdbuscpp.Error"};
96}
97
98#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
99 throw sdbus::createError((_ERRNO), (_MSG)) \
100
101
102#define SDBUS_THROW_ERROR_IF(_COND, _MSG, _ERRNO) \
103 if (!(_COND)) ; else SDBUS_THROW_ERROR((_MSG), (_ERRNO)) \
104
105
106#endif /* SDBUS_CXX_ERROR_H_ */
Definition Error.h:48
Definition Error.h:44