blob: 1bfd89068a773bf487e53c72587495f859cbb1ab [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 Williams15228662021-09-03 06:04:25 -050019int generated_exception::get_errno() const noexcept
20{
21 return EIO;
22}
23
Ed Tanoused4a5a62023-01-05 09:38:50 -080024SdBusError::SdBusError(int error_in, const char* prefix,
25 SdBusInterface* intf_in) :
Patrick Williams70bcf142023-07-26 16:19:48 -050026 SdBusError(error_in, std::string(prefix), intf_in)
27{}
28
29SdBusError::SdBusError(int error_in, std::string&& prefix,
30 SdBusInterface* intf_in) :
Ed Tanoused4a5a62023-01-05 09:38:50 -080031 error(SD_BUS_ERROR_NULL),
32 intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070033{
William A. Kennington III2726c172018-06-22 19:39:04 -070034 // We can't check the output of intf->sd_bus_error_set_errno() because
35 // it returns the input errorcode. We don't want to try and guess
36 // possible error statuses. Instead, check to see if the error was
37 // constructed to determine success.
Ed Tanoused4a5a62023-01-05 09:38:50 -080038 intf->sd_bus_error_set_errno(&this->error, error_in);
William A. Kennington III2726c172018-06-22 19:39:04 -070039 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070040 {
William A. Kennington III2726c172018-06-22 19:39:04 -070041 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070042 }
43
Patrick Williams70bcf142023-07-26 16:19:48 -050044 populateMessage(std::move(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070045}
46
Ed Tanoused4a5a62023-01-05 09:38:50 -080047SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
48 SdBusInterface* intf_in) :
49 error(*error_in),
50 intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070051{
William A. Kennington III68cb1702018-06-22 19:35:48 -070052 // We own the error so remove the caller's reference
Ed Tanoused4a5a62023-01-05 09:38:50 -080053 *error_in = SD_BUS_ERROR_NULL;
William A. Kennington III68cb1702018-06-22 19:35:48 -070054
Patrick Williams70bcf142023-07-26 16:19:48 -050055 populateMessage(std::string(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070056}
57
Vernon Maueryfac43a62018-08-01 12:29:23 -070058SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070059{
60 move(std::move(other));
61}
62
63SdBusError& SdBusError::operator=(SdBusError&& other)
64{
65 if (this != &other)
66 {
67 move(std::move(other));
68 }
69 return *this;
70}
71
72SdBusError::~SdBusError()
73{
William A. Kennington III37657502018-06-22 19:00:05 -070074 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070075}
76
77const char* SdBusError::name() const noexcept
78{
79 return error.name;
80}
81
82const char* SdBusError::description() const noexcept
83{
84 return error.message;
85}
86
87const char* SdBusError::what() const noexcept
88{
89 return full_message.c_str();
90}
91
Adriana Kobylak33335b32018-09-21 11:50:42 -050092int SdBusError::get_errno() const noexcept
93{
94 return intf->sd_bus_error_get_errno(&this->error);
95}
96
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010097const sd_bus_error* SdBusError::get_error() const noexcept
98{
99 return &error;
100}
101
Patrick Williams70bcf142023-07-26 16:19:48 -0500102void SdBusError::populateMessage(std::string&& prefix)
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700103{
Patrick Williams70bcf142023-07-26 16:19:48 -0500104 full_message = std::move(prefix);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700105 if (error.name)
106 {
107 full_message += ": ";
108 full_message += error.name;
109 }
110 if (error.message)
111 {
112 full_message += ": ";
113 full_message += error.message;
114 }
115}
116
117void SdBusError::move(SdBusError&& other)
118{
William A. Kennington III37657502018-06-22 19:00:05 -0700119 intf = std::move(other.intf);
120
121 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700122 error = other.error;
123 other.error = SD_BUS_ERROR_NULL;
124
125 full_message = std::move(other.full_message);
126}
127
Patrick Williams93b0e702017-04-18 11:19:45 -0500128const char* InvalidEnumString::name() const noexcept
129{
130 return errName;
131}
132
133const char* InvalidEnumString::description() const noexcept
134{
135 return errDesc;
136}
137
138const char* InvalidEnumString::what() const noexcept
139{
140 return errWhat;
141}
142
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500143int InvalidEnumString::get_errno() const noexcept
144{
145 return EINVAL;
146}
147
Ed Tanousc3484732023-01-04 15:59:18 -0800148static std::string unpackErrorReasonToString(const UnpackErrorReason reason)
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100149{
150 switch (reason)
151 {
152 case UnpackErrorReason::missingProperty:
153 return "Missing property";
154 case UnpackErrorReason::wrongType:
155 return "Type not matched";
156 }
157 return "Unknown";
158}
159
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200160UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100161 const UnpackErrorReason reasonIn) :
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200162 propertyName(propertyNameIn),
163 reason(reasonIn),
164 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100165 "', Reason: '" + unpackErrorReasonToString(reason) + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200166{}
167
168const char* UnpackPropertyError::name() const noexcept
169{
170 return errName;
171}
172
173const char* UnpackPropertyError::description() const noexcept
174{
175 return errDesc;
176}
177
178const char* UnpackPropertyError::what() const noexcept
179{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200180 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200181}
182
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500183int UnpackPropertyError::get_errno() const noexcept
184{
185 return EINVAL;
186}
187
Patrick Williams4cfc2842022-09-22 09:53:33 -0500188const char* UnhandledStop::name() const noexcept
189{
190 return errName;
191}
192
193const char* UnhandledStop::description() const noexcept
194{
195 return errDesc;
196}
197
198const char* UnhandledStop::what() const noexcept
199{
200 return errWhat;
201}
202
203int UnhandledStop::get_errno() const noexcept
204{
205 return ECANCELED;
206}
207
Patrick Williams93b0e702017-04-18 11:19:45 -0500208} // namespace exception
209} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500210
211#ifdef __clang__
212#pragma clang diagnostic pop
213#endif