blob: 565e059d19aea40b0d7d9cc9af1ffc27522c9786 [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 Williamsf289c652022-08-26 10:43:10 -050017void exception::unused() const noexcept {}
Patrick Williamsa8e35022021-09-28 06:16:52 -050018
Patrick Williamsa4bfefd2024-09-24 21:07:25 -040019int exception::set_error(sd_bus_error* e) const
20{
21 return sd_bus_error_set(e, name(), description());
22}
23
24int exception::set_error(SdBusInterface* i, sd_bus_error* e) const
25{
26 return i->sd_bus_error_set(e, name(), description());
27}
28
Patrick Williams15228662021-09-03 06:04:25 -050029int generated_exception::get_errno() const noexcept
30{
31 return EIO;
32}
33
Ed Tanoused4a5a62023-01-05 09:38:50 -080034SdBusError::SdBusError(int error_in, const char* prefix,
35 SdBusInterface* intf_in) :
Patrick Williams70bcf142023-07-26 16:19:48 -050036 SdBusError(error_in, std::string(prefix), intf_in)
37{}
38
39SdBusError::SdBusError(int error_in, std::string&& prefix,
40 SdBusInterface* intf_in) :
Patrick Williams06f265f2024-08-16 15:19:49 -040041 error(SD_BUS_ERROR_NULL), intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070042{
William A. Kennington III2726c172018-06-22 19:39:04 -070043 // We can't check the output of intf->sd_bus_error_set_errno() because
44 // it returns the input errorcode. We don't want to try and guess
45 // possible error statuses. Instead, check to see if the error was
46 // constructed to determine success.
Ed Tanoused4a5a62023-01-05 09:38:50 -080047 intf->sd_bus_error_set_errno(&this->error, error_in);
William A. Kennington III2726c172018-06-22 19:39:04 -070048 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070049 {
William A. Kennington III2726c172018-06-22 19:39:04 -070050 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070051 }
52
Patrick Williams70bcf142023-07-26 16:19:48 -050053 populateMessage(std::move(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070054}
55
Ed Tanoused4a5a62023-01-05 09:38:50 -080056SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
57 SdBusInterface* intf_in) :
Patrick Williams06f265f2024-08-16 15:19:49 -040058 error(*error_in), intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070059{
William A. Kennington III68cb1702018-06-22 19:35:48 -070060 // We own the error so remove the caller's reference
Ed Tanoused4a5a62023-01-05 09:38:50 -080061 *error_in = SD_BUS_ERROR_NULL;
William A. Kennington III68cb1702018-06-22 19:35:48 -070062
Patrick Williams70bcf142023-07-26 16:19:48 -050063 populateMessage(std::string(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070064}
65
Vernon Maueryfac43a62018-08-01 12:29:23 -070066SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070067{
68 move(std::move(other));
69}
70
71SdBusError& SdBusError::operator=(SdBusError&& other)
72{
73 if (this != &other)
74 {
75 move(std::move(other));
76 }
77 return *this;
78}
79
80SdBusError::~SdBusError()
81{
William A. Kennington III37657502018-06-22 19:00:05 -070082 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070083}
84
85const char* SdBusError::name() const noexcept
86{
87 return error.name;
88}
89
90const char* SdBusError::description() const noexcept
91{
92 return error.message;
93}
94
95const char* SdBusError::what() const noexcept
96{
97 return full_message.c_str();
98}
99
Adriana Kobylak33335b32018-09-21 11:50:42 -0500100int SdBusError::get_errno() const noexcept
101{
102 return intf->sd_bus_error_get_errno(&this->error);
103}
104
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +0100105const sd_bus_error* SdBusError::get_error() const noexcept
106{
107 return &error;
108}
109
Patrick Williams70bcf142023-07-26 16:19:48 -0500110void SdBusError::populateMessage(std::string&& prefix)
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700111{
Patrick Williams70bcf142023-07-26 16:19:48 -0500112 full_message = std::move(prefix);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700113 if (error.name)
114 {
115 full_message += ": ";
116 full_message += error.name;
117 }
118 if (error.message)
119 {
120 full_message += ": ";
121 full_message += error.message;
122 }
123}
124
125void SdBusError::move(SdBusError&& other)
126{
William A. Kennington III37657502018-06-22 19:00:05 -0700127 intf = std::move(other.intf);
128
129 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700130 error = other.error;
131 other.error = SD_BUS_ERROR_NULL;
132
133 full_message = std::move(other.full_message);
134}
135
Patrick Williams93b0e702017-04-18 11:19:45 -0500136const char* InvalidEnumString::name() const noexcept
137{
138 return errName;
139}
140
141const char* InvalidEnumString::description() const noexcept
142{
143 return errDesc;
144}
145
146const char* InvalidEnumString::what() const noexcept
147{
148 return errWhat;
149}
150
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500151int InvalidEnumString::get_errno() const noexcept
152{
153 return EINVAL;
154}
155
Ed Tanousc3484732023-01-04 15:59:18 -0800156static std::string unpackErrorReasonToString(const UnpackErrorReason reason)
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100157{
158 switch (reason)
159 {
160 case UnpackErrorReason::missingProperty:
161 return "Missing property";
162 case UnpackErrorReason::wrongType:
163 return "Type not matched";
164 }
165 return "Unknown";
166}
167
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200168UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100169 const UnpackErrorReason reasonIn) :
Patrick Williams06f265f2024-08-16 15:19:49 -0400170 propertyName(propertyNameIn), reason(reasonIn),
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200171 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100172 "', Reason: '" + unpackErrorReasonToString(reason) + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200173{}
174
175const char* UnpackPropertyError::name() const noexcept
176{
177 return errName;
178}
179
180const char* UnpackPropertyError::description() const noexcept
181{
182 return errDesc;
183}
184
185const char* UnpackPropertyError::what() const noexcept
186{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200187 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200188}
189
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500190int UnpackPropertyError::get_errno() const noexcept
191{
192 return EINVAL;
193}
194
Patrick Williams4cfc2842022-09-22 09:53:33 -0500195const char* UnhandledStop::name() const noexcept
196{
197 return errName;
198}
199
200const char* UnhandledStop::description() const noexcept
201{
202 return errDesc;
203}
204
205const char* UnhandledStop::what() const noexcept
206{
207 return errWhat;
208}
209
210int UnhandledStop::get_errno() const noexcept
211{
212 return ECANCELED;
213}
214
Patrick Williams93b0e702017-04-18 11:19:45 -0500215} // namespace exception
216} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500217
218#ifdef __clang__
219#pragma clang diagnostic pop
220#endif