blob: b58bd01abba793ecfdb800a8e9ab215fdff0620b [file] [log] [blame]
Patrick Williams93b0e702017-04-18 11:19:45 -05001#include <sdbusplus/exception.hpp>
Patrick Williams127b8ab2020-05-21 15:24:19 -05002
William A. Kennington III2726c172018-06-22 19:39:04 -07003#include <stdexcept>
William A. Kennington III2726c172018-06-22 19:39:04 -07004#include <utility>
William A. Kennington III37657502018-06-22 19:00:05 -07005
Patrick Williams0c8136a2021-08-28 14:42:31 -05006#ifdef __clang__
7#pragma clang diagnostic push
8#pragma clang diagnostic ignored "-Wc99-extensions"
9#endif
10
Patrick Williams93b0e702017-04-18 11:19:45 -050011namespace sdbusplus
12{
13namespace exception
14{
15
William A. Kennington III37657502018-06-22 19:00:05 -070016SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070017 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070018{
William A. Kennington III2726c172018-06-22 19:39:04 -070019 // We can't check the output of intf->sd_bus_error_set_errno() because
20 // it returns the input errorcode. We don't want to try and guess
21 // possible error statuses. Instead, check to see if the error was
22 // constructed to determine success.
23 intf->sd_bus_error_set_errno(&this->error, error);
24 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070025 {
William A. Kennington III2726c172018-06-22 19:39:04 -070026 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070027 }
28
29 populateMessage(prefix);
30}
31
William A. Kennington III68cb1702018-06-22 19:35:48 -070032SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070033 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070034 error(*error),
35 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070036{
William A. Kennington III68cb1702018-06-22 19:35:48 -070037 // We own the error so remove the caller's reference
38 *error = SD_BUS_ERROR_NULL;
39
William A. Kennington III20db3bf2018-05-14 16:18:37 -070040 populateMessage(prefix);
41}
42
Vernon Maueryfac43a62018-08-01 12:29:23 -070043SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070044{
45 move(std::move(other));
46}
47
48SdBusError& SdBusError::operator=(SdBusError&& other)
49{
50 if (this != &other)
51 {
52 move(std::move(other));
53 }
54 return *this;
55}
56
57SdBusError::~SdBusError()
58{
William A. Kennington III37657502018-06-22 19:00:05 -070059 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070060}
61
62const char* SdBusError::name() const noexcept
63{
64 return error.name;
65}
66
67const char* SdBusError::description() const noexcept
68{
69 return error.message;
70}
71
72const char* SdBusError::what() const noexcept
73{
74 return full_message.c_str();
75}
76
Adriana Kobylak33335b32018-09-21 11:50:42 -050077int SdBusError::get_errno() const noexcept
78{
79 return intf->sd_bus_error_get_errno(&this->error);
80}
81
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010082const sd_bus_error* SdBusError::get_error() const noexcept
83{
84 return &error;
85}
86
William A. Kennington III20db3bf2018-05-14 16:18:37 -070087void SdBusError::populateMessage(const char* prefix)
88{
89 full_message = prefix;
90 if (error.name)
91 {
92 full_message += ": ";
93 full_message += error.name;
94 }
95 if (error.message)
96 {
97 full_message += ": ";
98 full_message += error.message;
99 }
100}
101
102void SdBusError::move(SdBusError&& other)
103{
William A. Kennington III37657502018-06-22 19:00:05 -0700104 intf = std::move(other.intf);
105
106 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700107 error = other.error;
108 other.error = SD_BUS_ERROR_NULL;
109
110 full_message = std::move(other.full_message);
111}
112
Patrick Williams93b0e702017-04-18 11:19:45 -0500113const char* InvalidEnumString::name() const noexcept
114{
115 return errName;
116}
117
118const char* InvalidEnumString::description() const noexcept
119{
120 return errDesc;
121}
122
123const char* InvalidEnumString::what() const noexcept
124{
125 return errWhat;
126}
127
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200128UnpackPropertyError::UnpackPropertyError(std::string_view propertyName,
129 std::string_view reason) :
130 propertyName(propertyName),
131 reason(reason)
132{}
133
134const char* UnpackPropertyError::name() const noexcept
135{
136 return errName;
137}
138
139const char* UnpackPropertyError::description() const noexcept
140{
141 return errDesc;
142}
143
144const char* UnpackPropertyError::what() const noexcept
145{
146 return errWhat;
147}
148
Patrick Williams93b0e702017-04-18 11:19:45 -0500149} // namespace exception
150} // namespace sdbusplus
Patrick Williams0c8136a2021-08-28 14:42:31 -0500151
152#ifdef __clang__
153#pragma clang diagnostic pop
154#endif