blob: a7a359645585694aebf762411f37be343745364c [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) :
26 error(SD_BUS_ERROR_NULL),
27 intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070028{
William A. Kennington III2726c172018-06-22 19:39:04 -070029 // We can't check the output of intf->sd_bus_error_set_errno() because
30 // it returns the input errorcode. We don't want to try and guess
31 // possible error statuses. Instead, check to see if the error was
32 // constructed to determine success.
Ed Tanoused4a5a62023-01-05 09:38:50 -080033 intf->sd_bus_error_set_errno(&this->error, error_in);
William A. Kennington III2726c172018-06-22 19:39:04 -070034 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070035 {
William A. Kennington III2726c172018-06-22 19:39:04 -070036 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070037 }
38
39 populateMessage(prefix);
40}
41
Ed Tanoused4a5a62023-01-05 09:38:50 -080042SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
43 SdBusInterface* intf_in) :
44 error(*error_in),
45 intf(intf_in)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070046{
William A. Kennington III68cb1702018-06-22 19:35:48 -070047 // We own the error so remove the caller's reference
Ed Tanoused4a5a62023-01-05 09:38:50 -080048 *error_in = SD_BUS_ERROR_NULL;
William A. Kennington III68cb1702018-06-22 19:35:48 -070049
William A. Kennington III20db3bf2018-05-14 16:18:37 -070050 populateMessage(prefix);
51}
52
Vernon Maueryfac43a62018-08-01 12:29:23 -070053SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070054{
55 move(std::move(other));
56}
57
58SdBusError& SdBusError::operator=(SdBusError&& other)
59{
60 if (this != &other)
61 {
62 move(std::move(other));
63 }
64 return *this;
65}
66
67SdBusError::~SdBusError()
68{
William A. Kennington III37657502018-06-22 19:00:05 -070069 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070070}
71
72const char* SdBusError::name() const noexcept
73{
74 return error.name;
75}
76
77const char* SdBusError::description() const noexcept
78{
79 return error.message;
80}
81
82const char* SdBusError::what() const noexcept
83{
84 return full_message.c_str();
85}
86
Adriana Kobylak33335b32018-09-21 11:50:42 -050087int SdBusError::get_errno() const noexcept
88{
89 return intf->sd_bus_error_get_errno(&this->error);
90}
91
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010092const sd_bus_error* SdBusError::get_error() const noexcept
93{
94 return &error;
95}
96
William A. Kennington III20db3bf2018-05-14 16:18:37 -070097void SdBusError::populateMessage(const char* prefix)
98{
99 full_message = prefix;
100 if (error.name)
101 {
102 full_message += ": ";
103 full_message += error.name;
104 }
105 if (error.message)
106 {
107 full_message += ": ";
108 full_message += error.message;
109 }
110}
111
112void SdBusError::move(SdBusError&& other)
113{
William A. Kennington III37657502018-06-22 19:00:05 -0700114 intf = std::move(other.intf);
115
116 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700117 error = other.error;
118 other.error = SD_BUS_ERROR_NULL;
119
120 full_message = std::move(other.full_message);
121}
122
Patrick Williams93b0e702017-04-18 11:19:45 -0500123const char* InvalidEnumString::name() const noexcept
124{
125 return errName;
126}
127
128const char* InvalidEnumString::description() const noexcept
129{
130 return errDesc;
131}
132
133const char* InvalidEnumString::what() const noexcept
134{
135 return errWhat;
136}
137
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500138int InvalidEnumString::get_errno() const noexcept
139{
140 return EINVAL;
141}
142
Ed Tanousc3484732023-01-04 15:59:18 -0800143static std::string unpackErrorReasonToString(const UnpackErrorReason reason)
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100144{
145 switch (reason)
146 {
147 case UnpackErrorReason::missingProperty:
148 return "Missing property";
149 case UnpackErrorReason::wrongType:
150 return "Type not matched";
151 }
152 return "Unknown";
153}
154
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200155UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100156 const UnpackErrorReason reasonIn) :
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200157 propertyName(propertyNameIn),
158 reason(reasonIn),
159 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100160 "', Reason: '" + unpackErrorReasonToString(reason) + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200161{}
162
163const char* UnpackPropertyError::name() const noexcept
164{
165 return errName;
166}
167
168const char* UnpackPropertyError::description() const noexcept
169{
170 return errDesc;
171}
172
173const char* UnpackPropertyError::what() const noexcept
174{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200175 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200176}
177
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500178int UnpackPropertyError::get_errno() const noexcept
179{
180 return EINVAL;
181}
182
Patrick Williams4cfc2842022-09-22 09:53:33 -0500183const char* UnhandledStop::name() const noexcept
184{
185 return errName;
186}
187
188const char* UnhandledStop::description() const noexcept
189{
190 return errDesc;
191}
192
193const char* UnhandledStop::what() const noexcept
194{
195 return errWhat;
196}
197
198int UnhandledStop::get_errno() const noexcept
199{
200 return ECANCELED;
201}
202
Patrick Williams93b0e702017-04-18 11:19:45 -0500203} // namespace exception
204} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500205
206#ifdef __clang__
207#pragma clang diagnostic pop
208#endif