blob: 14071472ab8a6e6c9d0e9b92c7af4f5e46e304d1 [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
William A. Kennington III37657502018-06-22 19:00:05 -070024SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070025 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070026{
William A. Kennington III2726c172018-06-22 19:39:04 -070027 // We can't check the output of intf->sd_bus_error_set_errno() because
28 // it returns the input errorcode. We don't want to try and guess
29 // possible error statuses. Instead, check to see if the error was
30 // constructed to determine success.
31 intf->sd_bus_error_set_errno(&this->error, error);
32 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070033 {
William A. Kennington III2726c172018-06-22 19:39:04 -070034 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070035 }
36
37 populateMessage(prefix);
38}
39
William A. Kennington III68cb1702018-06-22 19:35:48 -070040SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070041 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070042 error(*error),
43 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070044{
William A. Kennington III68cb1702018-06-22 19:35:48 -070045 // We own the error so remove the caller's reference
46 *error = SD_BUS_ERROR_NULL;
47
William A. Kennington III20db3bf2018-05-14 16:18:37 -070048 populateMessage(prefix);
49}
50
Vernon Maueryfac43a62018-08-01 12:29:23 -070051SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070052{
53 move(std::move(other));
54}
55
56SdBusError& SdBusError::operator=(SdBusError&& other)
57{
58 if (this != &other)
59 {
60 move(std::move(other));
61 }
62 return *this;
63}
64
65SdBusError::~SdBusError()
66{
William A. Kennington III37657502018-06-22 19:00:05 -070067 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070068}
69
70const char* SdBusError::name() const noexcept
71{
72 return error.name;
73}
74
75const char* SdBusError::description() const noexcept
76{
77 return error.message;
78}
79
80const char* SdBusError::what() const noexcept
81{
82 return full_message.c_str();
83}
84
Adriana Kobylak33335b32018-09-21 11:50:42 -050085int SdBusError::get_errno() const noexcept
86{
87 return intf->sd_bus_error_get_errno(&this->error);
88}
89
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010090const sd_bus_error* SdBusError::get_error() const noexcept
91{
92 return &error;
93}
94
William A. Kennington III20db3bf2018-05-14 16:18:37 -070095void SdBusError::populateMessage(const char* prefix)
96{
97 full_message = prefix;
98 if (error.name)
99 {
100 full_message += ": ";
101 full_message += error.name;
102 }
103 if (error.message)
104 {
105 full_message += ": ";
106 full_message += error.message;
107 }
108}
109
110void SdBusError::move(SdBusError&& other)
111{
William A. Kennington III37657502018-06-22 19:00:05 -0700112 intf = std::move(other.intf);
113
114 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700115 error = other.error;
116 other.error = SD_BUS_ERROR_NULL;
117
118 full_message = std::move(other.full_message);
119}
120
Patrick Williams93b0e702017-04-18 11:19:45 -0500121const char* InvalidEnumString::name() const noexcept
122{
123 return errName;
124}
125
126const char* InvalidEnumString::description() const noexcept
127{
128 return errDesc;
129}
130
131const char* InvalidEnumString::what() const noexcept
132{
133 return errWhat;
134}
135
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500136int InvalidEnumString::get_errno() const noexcept
137{
138 return EINVAL;
139}
140
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100141std::string unpackErrorReasonToString(const UnpackErrorReason reason)
142{
143 switch (reason)
144 {
145 case UnpackErrorReason::missingProperty:
146 return "Missing property";
147 case UnpackErrorReason::wrongType:
148 return "Type not matched";
149 }
150 return "Unknown";
151}
152
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200153UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100154 const UnpackErrorReason reasonIn) :
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200155 propertyName(propertyNameIn),
156 reason(reasonIn),
157 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100158 "', Reason: '" + unpackErrorReasonToString(reason) + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200159{}
160
161const char* UnpackPropertyError::name() const noexcept
162{
163 return errName;
164}
165
166const char* UnpackPropertyError::description() const noexcept
167{
168 return errDesc;
169}
170
171const char* UnpackPropertyError::what() const noexcept
172{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200173 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200174}
175
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500176int UnpackPropertyError::get_errno() const noexcept
177{
178 return EINVAL;
179}
180
Patrick Williams93b0e702017-04-18 11:19:45 -0500181} // namespace exception
182} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500183
184#ifdef __clang__
185#pragma clang diagnostic pop
186#endif