blob: 4a04dea4a401a619573599e52c279944b4bbf300 [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 Williamsa8e35022021-09-28 06:16:52 -050017void exception::unused() const noexcept
18{}
19
Patrick Williams15228662021-09-03 06:04:25 -050020int generated_exception::get_errno() const noexcept
21{
22 return EIO;
23}
24
William A. Kennington III37657502018-06-22 19:00:05 -070025SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070026 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070027{
William A. Kennington III2726c172018-06-22 19:39:04 -070028 // We can't check the output of intf->sd_bus_error_set_errno() because
29 // it returns the input errorcode. We don't want to try and guess
30 // possible error statuses. Instead, check to see if the error was
31 // constructed to determine success.
32 intf->sd_bus_error_set_errno(&this->error, error);
33 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070034 {
William A. Kennington III2726c172018-06-22 19:39:04 -070035 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070036 }
37
38 populateMessage(prefix);
39}
40
William A. Kennington III68cb1702018-06-22 19:35:48 -070041SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070042 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070043 error(*error),
44 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070045{
William A. Kennington III68cb1702018-06-22 19:35:48 -070046 // We own the error so remove the caller's reference
47 *error = SD_BUS_ERROR_NULL;
48
William A. Kennington III20db3bf2018-05-14 16:18:37 -070049 populateMessage(prefix);
50}
51
Vernon Maueryfac43a62018-08-01 12:29:23 -070052SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070053{
54 move(std::move(other));
55}
56
57SdBusError& SdBusError::operator=(SdBusError&& other)
58{
59 if (this != &other)
60 {
61 move(std::move(other));
62 }
63 return *this;
64}
65
66SdBusError::~SdBusError()
67{
William A. Kennington III37657502018-06-22 19:00:05 -070068 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070069}
70
71const char* SdBusError::name() const noexcept
72{
73 return error.name;
74}
75
76const char* SdBusError::description() const noexcept
77{
78 return error.message;
79}
80
81const char* SdBusError::what() const noexcept
82{
83 return full_message.c_str();
84}
85
Adriana Kobylak33335b32018-09-21 11:50:42 -050086int SdBusError::get_errno() const noexcept
87{
88 return intf->sd_bus_error_get_errno(&this->error);
89}
90
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010091const sd_bus_error* SdBusError::get_error() const noexcept
92{
93 return &error;
94}
95
William A. Kennington III20db3bf2018-05-14 16:18:37 -070096void SdBusError::populateMessage(const char* prefix)
97{
98 full_message = prefix;
99 if (error.name)
100 {
101 full_message += ": ";
102 full_message += error.name;
103 }
104 if (error.message)
105 {
106 full_message += ": ";
107 full_message += error.message;
108 }
109}
110
111void SdBusError::move(SdBusError&& other)
112{
William A. Kennington III37657502018-06-22 19:00:05 -0700113 intf = std::move(other.intf);
114
115 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700116 error = other.error;
117 other.error = SD_BUS_ERROR_NULL;
118
119 full_message = std::move(other.full_message);
120}
121
Patrick Williams93b0e702017-04-18 11:19:45 -0500122const char* InvalidEnumString::name() const noexcept
123{
124 return errName;
125}
126
127const char* InvalidEnumString::description() const noexcept
128{
129 return errDesc;
130}
131
132const char* InvalidEnumString::what() const noexcept
133{
134 return errWhat;
135}
136
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500137int InvalidEnumString::get_errno() const noexcept
138{
139 return EINVAL;
140}
141
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200142UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
143 std::string_view reasonIn) :
144 propertyName(propertyNameIn),
145 reason(reasonIn),
146 errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
147 "', Reason: '" + reason + "'.")
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200148{}
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{
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200162 return errWhatDetailed.c_str();
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200163}
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