blob: 640454af834701f755f24ca0fa0a0e9fc75d8636 [file] [log] [blame]
Patrick Williams93b0e702017-04-18 11:19:45 -05001#include <sdbusplus/exception.hpp>
Patrick Williams127b8ab2020-05-21 15:24:19 -05002
Patrick Williamsb6f729d2021-09-02 15:02:19 -05003#include <cerrno>
William A. Kennington III2726c172018-06-22 19:39:04 -07004#include <stdexcept>
William A. Kennington III2726c172018-06-22 19:39:04 -07005#include <utility>
William A. Kennington III37657502018-06-22 19:00:05 -07006
Patrick Williams0c8136a2021-08-28 14:42:31 -05007#ifdef __clang__
8#pragma clang diagnostic push
9#pragma clang diagnostic ignored "-Wc99-extensions"
10#endif
11
Patrick Williams93b0e702017-04-18 11:19:45 -050012namespace sdbusplus
13{
14namespace exception
15{
16
Patrick Williamsb6f729d2021-09-02 15:02:19 -050017int exception::get_errno() const noexcept
18{
19 return EIO;
20}
21
William A. Kennington III37657502018-06-22 19:00:05 -070022SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070023 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070024{
William A. Kennington III2726c172018-06-22 19:39:04 -070025 // We can't check the output of intf->sd_bus_error_set_errno() because
26 // it returns the input errorcode. We don't want to try and guess
27 // possible error statuses. Instead, check to see if the error was
28 // constructed to determine success.
29 intf->sd_bus_error_set_errno(&this->error, error);
30 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070031 {
William A. Kennington III2726c172018-06-22 19:39:04 -070032 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070033 }
34
35 populateMessage(prefix);
36}
37
William A. Kennington III68cb1702018-06-22 19:35:48 -070038SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070039 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070040 error(*error),
41 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070042{
William A. Kennington III68cb1702018-06-22 19:35:48 -070043 // We own the error so remove the caller's reference
44 *error = SD_BUS_ERROR_NULL;
45
William A. Kennington III20db3bf2018-05-14 16:18:37 -070046 populateMessage(prefix);
47}
48
Vernon Maueryfac43a62018-08-01 12:29:23 -070049SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070050{
51 move(std::move(other));
52}
53
54SdBusError& SdBusError::operator=(SdBusError&& other)
55{
56 if (this != &other)
57 {
58 move(std::move(other));
59 }
60 return *this;
61}
62
63SdBusError::~SdBusError()
64{
William A. Kennington III37657502018-06-22 19:00:05 -070065 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070066}
67
68const char* SdBusError::name() const noexcept
69{
70 return error.name;
71}
72
73const char* SdBusError::description() const noexcept
74{
75 return error.message;
76}
77
78const char* SdBusError::what() const noexcept
79{
80 return full_message.c_str();
81}
82
Adriana Kobylak33335b32018-09-21 11:50:42 -050083int SdBusError::get_errno() const noexcept
84{
85 return intf->sd_bus_error_get_errno(&this->error);
86}
87
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010088const sd_bus_error* SdBusError::get_error() const noexcept
89{
90 return &error;
91}
92
William A. Kennington III20db3bf2018-05-14 16:18:37 -070093void SdBusError::populateMessage(const char* prefix)
94{
95 full_message = prefix;
96 if (error.name)
97 {
98 full_message += ": ";
99 full_message += error.name;
100 }
101 if (error.message)
102 {
103 full_message += ": ";
104 full_message += error.message;
105 }
106}
107
108void SdBusError::move(SdBusError&& other)
109{
William A. Kennington III37657502018-06-22 19:00:05 -0700110 intf = std::move(other.intf);
111
112 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700113 error = other.error;
114 other.error = SD_BUS_ERROR_NULL;
115
116 full_message = std::move(other.full_message);
117}
118
Patrick Williams93b0e702017-04-18 11:19:45 -0500119const char* InvalidEnumString::name() const noexcept
120{
121 return errName;
122}
123
124const char* InvalidEnumString::description() const noexcept
125{
126 return errDesc;
127}
128
129const char* InvalidEnumString::what() const noexcept
130{
131 return errWhat;
132}
133
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500134int InvalidEnumString::get_errno() const noexcept
135{
136 return EINVAL;
137}
138
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200139UnpackPropertyError::UnpackPropertyError(std::string_view propertyName,
140 std::string_view reason) :
141 propertyName(propertyName),
142 reason(reason)
143{}
144
145const char* UnpackPropertyError::name() const noexcept
146{
147 return errName;
148}
149
150const char* UnpackPropertyError::description() const noexcept
151{
152 return errDesc;
153}
154
155const char* UnpackPropertyError::what() const noexcept
156{
157 return errWhat;
158}
159
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500160int UnpackPropertyError::get_errno() const noexcept
161{
162 return EINVAL;
163}
164
Patrick Williams93b0e702017-04-18 11:19:45 -0500165} // namespace exception
166} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500167
168#ifdef __clang__
169#pragma clang diagnostic pop
170#endif