blob: c03e2bf9564173c58f6dfb3f9707f5b8ed904751 [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) :
Patrick Williams06f265f2024-08-16 15:19:49 -040031 error(SD_BUS_ERROR_NULL), intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070032{
William A. Kennington III2726c172018-06-22 19:39:04 -070033 // We can't check the output of intf->sd_bus_error_set_errno() because
34 // it returns the input errorcode. We don't want to try and guess
35 // possible error statuses. Instead, check to see if the error was
36 // constructed to determine success.
Ed Tanoused4a5a62023-01-05 09:38:50 -080037 intf->sd_bus_error_set_errno(&this->error, error_in);
William A. Kennington III2726c172018-06-22 19:39:04 -070038 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070039 {
William A. Kennington III2726c172018-06-22 19:39:04 -070040 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070041 }
42
Patrick Williams70bcf142023-07-26 16:19:48 -050043 populateMessage(std::move(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070044}
45
Ed Tanoused4a5a62023-01-05 09:38:50 -080046SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
47 SdBusInterface* intf_in) :
Patrick Williams06f265f2024-08-16 15:19:49 -040048 error(*error_in), intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070049{
William A. Kennington III68cb1702018-06-22 19:35:48 -070050 // We own the error so remove the caller's reference
Ed Tanoused4a5a62023-01-05 09:38:50 -080051 *error_in = SD_BUS_ERROR_NULL;
William A. Kennington III68cb1702018-06-22 19:35:48 -070052
Patrick Williams70bcf142023-07-26 16:19:48 -050053 populateMessage(std::string(prefix));
William A. Kennington III20db3bf2018-05-14 16:18:37 -070054}
55
Vernon Maueryfac43a62018-08-01 12:29:23 -070056SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070057{
58 move(std::move(other));
59}
60
61SdBusError& SdBusError::operator=(SdBusError&& other)
62{
63 if (this != &other)
64 {
65 move(std::move(other));
66 }
67 return *this;
68}
69
70SdBusError::~SdBusError()
71{
William A. Kennington III37657502018-06-22 19:00:05 -070072 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070073}
74
75const char* SdBusError::name() const noexcept
76{
77 return error.name;
78}
79
80const char* SdBusError::description() const noexcept
81{
82 return error.message;
83}
84
85const char* SdBusError::what() const noexcept
86{
87 return full_message.c_str();
88}
89
Adriana Kobylak33335b32018-09-21 11:50:42 -050090int SdBusError::get_errno() const noexcept
91{
92 return intf->sd_bus_error_get_errno(&this->error);
93}
94
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010095const sd_bus_error* SdBusError::get_error() const noexcept
96{
97 return &error;
98}
99
Patrick Williams70bcf142023-07-26 16:19:48 -0500100void SdBusError::populateMessage(std::string&& prefix)
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700101{
Patrick Williams70bcf142023-07-26 16:19:48 -0500102 full_message = std::move(prefix);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700103 if (error.name)
104 {
105 full_message += ": ";
106 full_message += error.name;
107 }
108 if (error.message)
109 {
110 full_message += ": ";
111 full_message += error.message;
112 }
113}
114
115void SdBusError::move(SdBusError&& other)
116{
William A. Kennington III37657502018-06-22 19:00:05 -0700117 intf = std::move(other.intf);
118
119 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700120 error = other.error;
121 other.error = SD_BUS_ERROR_NULL;
122
123 full_message = std::move(other.full_message);
124}
125
Patrick Williams93b0e702017-04-18 11:19:45 -0500126const char* InvalidEnumString::name() const noexcept
127{
128 return errName;
129}
130
131const char* InvalidEnumString::description() const noexcept
132{
133 return errDesc;
134}
135
136const char* InvalidEnumString::what() const noexcept
137{
138 return errWhat;
139}
140
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500141int InvalidEnumString::get_errno() const noexcept
142{
143 return EINVAL;
144}
145
Ed Tanousc3484732023-01-04 15:59:18 -0800146static std::string unpackErrorReasonToString(const UnpackErrorReason reason)
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100147{
148 switch (reason)
149 {
150 case UnpackErrorReason::missingProperty:
151 return "Missing property";
152 case UnpackErrorReason::wrongType:
153 return "Type not matched";
154 }
155 return "Unknown";
156}
157
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200158UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100159 const UnpackErrorReason reasonIn) :
Patrick Williams06f265f2024-08-16 15:19:49 -0400160 propertyName(propertyNameIn), reason(reasonIn),
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200161 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100162 "', Reason: '" + unpackErrorReasonToString(reason) + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200163{}
164
165const char* UnpackPropertyError::name() const noexcept
166{
167 return errName;
168}
169
170const char* UnpackPropertyError::description() const noexcept
171{
172 return errDesc;
173}
174
175const char* UnpackPropertyError::what() const noexcept
176{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200177 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200178}
179
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500180int UnpackPropertyError::get_errno() const noexcept
181{
182 return EINVAL;
183}
184
Patrick Williams4cfc2842022-09-22 09:53:33 -0500185const char* UnhandledStop::name() const noexcept
186{
187 return errName;
188}
189
190const char* UnhandledStop::description() const noexcept
191{
192 return errDesc;
193}
194
195const char* UnhandledStop::what() const noexcept
196{
197 return errWhat;
198}
199
200int UnhandledStop::get_errno() const noexcept
201{
202 return ECANCELED;
203}
204
Patrick Williams93b0e702017-04-18 11:19:45 -0500205} // namespace exception
206} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500207
208#ifdef __clang__
209#pragma clang diagnostic pop
210#endif