blob: 4fa51358842c665b6802b3d19deeb5dba300caa1 [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 Williamsb6f729d2021-09-02 15:02:19 -050017int exception::get_errno() const noexcept
18{
19 return EIO;
20}
21
Patrick Williams15228662021-09-03 06:04:25 -050022int generated_exception::get_errno() const noexcept
23{
24 return EIO;
25}
26
William A. Kennington III37657502018-06-22 19:00:05 -070027SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070028 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070029{
William A. Kennington III2726c172018-06-22 19:39:04 -070030 // We can't check the output of intf->sd_bus_error_set_errno() because
31 // it returns the input errorcode. We don't want to try and guess
32 // possible error statuses. Instead, check to see if the error was
33 // constructed to determine success.
34 intf->sd_bus_error_set_errno(&this->error, error);
35 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070036 {
William A. Kennington III2726c172018-06-22 19:39:04 -070037 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070038 }
39
40 populateMessage(prefix);
41}
42
William A. Kennington III68cb1702018-06-22 19:35:48 -070043SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070044 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070045 error(*error),
46 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070047{
William A. Kennington III68cb1702018-06-22 19:35:48 -070048 // We own the error so remove the caller's reference
49 *error = SD_BUS_ERROR_NULL;
50
William A. Kennington III20db3bf2018-05-14 16:18:37 -070051 populateMessage(prefix);
52}
53
Vernon Maueryfac43a62018-08-01 12:29:23 -070054SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070055{
56 move(std::move(other));
57}
58
59SdBusError& SdBusError::operator=(SdBusError&& other)
60{
61 if (this != &other)
62 {
63 move(std::move(other));
64 }
65 return *this;
66}
67
68SdBusError::~SdBusError()
69{
William A. Kennington III37657502018-06-22 19:00:05 -070070 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070071}
72
73const char* SdBusError::name() const noexcept
74{
75 return error.name;
76}
77
78const char* SdBusError::description() const noexcept
79{
80 return error.message;
81}
82
83const char* SdBusError::what() const noexcept
84{
85 return full_message.c_str();
86}
87
Adriana Kobylak33335b32018-09-21 11:50:42 -050088int SdBusError::get_errno() const noexcept
89{
90 return intf->sd_bus_error_get_errno(&this->error);
91}
92
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010093const sd_bus_error* SdBusError::get_error() const noexcept
94{
95 return &error;
96}
97
William A. Kennington III20db3bf2018-05-14 16:18:37 -070098void SdBusError::populateMessage(const char* prefix)
99{
100 full_message = prefix;
101 if (error.name)
102 {
103 full_message += ": ";
104 full_message += error.name;
105 }
106 if (error.message)
107 {
108 full_message += ": ";
109 full_message += error.message;
110 }
111}
112
113void SdBusError::move(SdBusError&& other)
114{
William A. Kennington III37657502018-06-22 19:00:05 -0700115 intf = std::move(other.intf);
116
117 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700118 error = other.error;
119 other.error = SD_BUS_ERROR_NULL;
120
121 full_message = std::move(other.full_message);
122}
123
Patrick Williams93b0e702017-04-18 11:19:45 -0500124const char* InvalidEnumString::name() const noexcept
125{
126 return errName;
127}
128
129const char* InvalidEnumString::description() const noexcept
130{
131 return errDesc;
132}
133
134const char* InvalidEnumString::what() const noexcept
135{
136 return errWhat;
137}
138
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500139int InvalidEnumString::get_errno() const noexcept
140{
141 return EINVAL;
142}
143
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200144UnpackPropertyError::UnpackPropertyError(std::string_view propertyName,
145 std::string_view reason) :
146 propertyName(propertyName),
147 reason(reason)
148{}
149
150const char* UnpackPropertyError::name() const noexcept
151{
152 return errName;
153}
154
155const char* UnpackPropertyError::description() const noexcept
156{
157 return errDesc;
158}
159
160const char* UnpackPropertyError::what() const noexcept
161{
162 return errWhat;
163}
164
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500165int UnpackPropertyError::get_errno() const noexcept
166{
167 return EINVAL;
168}
169
Patrick Williams93b0e702017-04-18 11:19:45 -0500170} // namespace exception
171} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500172
173#ifdef __clang__
174#pragma clang diagnostic pop
175#endif