blob: c13c5ddfa69f8d51a6e4a43b48e5d2e8dc41fc44 [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 Williams93b0e702017-04-18 11:19:45 -05006namespace sdbusplus
7{
8namespace exception
9{
10
William A. Kennington III37657502018-06-22 19:00:05 -070011SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070012 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070013{
William A. Kennington III2726c172018-06-22 19:39:04 -070014 // We can't check the output of intf->sd_bus_error_set_errno() because
15 // it returns the input errorcode. We don't want to try and guess
16 // possible error statuses. Instead, check to see if the error was
17 // constructed to determine success.
18 intf->sd_bus_error_set_errno(&this->error, error);
19 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070020 {
William A. Kennington III2726c172018-06-22 19:39:04 -070021 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070022 }
23
24 populateMessage(prefix);
25}
26
William A. Kennington III68cb1702018-06-22 19:35:48 -070027SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070028 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070029 error(*error),
30 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070031{
William A. Kennington III68cb1702018-06-22 19:35:48 -070032 // We own the error so remove the caller's reference
33 *error = SD_BUS_ERROR_NULL;
34
William A. Kennington III20db3bf2018-05-14 16:18:37 -070035 populateMessage(prefix);
36}
37
Vernon Maueryfac43a62018-08-01 12:29:23 -070038SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070039{
40 move(std::move(other));
41}
42
43SdBusError& SdBusError::operator=(SdBusError&& other)
44{
45 if (this != &other)
46 {
47 move(std::move(other));
48 }
49 return *this;
50}
51
52SdBusError::~SdBusError()
53{
William A. Kennington III37657502018-06-22 19:00:05 -070054 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070055}
56
57const char* SdBusError::name() const noexcept
58{
59 return error.name;
60}
61
62const char* SdBusError::description() const noexcept
63{
64 return error.message;
65}
66
67const char* SdBusError::what() const noexcept
68{
69 return full_message.c_str();
70}
71
Adriana Kobylak33335b32018-09-21 11:50:42 -050072int SdBusError::get_errno() const noexcept
73{
74 return intf->sd_bus_error_get_errno(&this->error);
75}
76
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010077const sd_bus_error* SdBusError::get_error() const noexcept
78{
79 return &error;
80}
81
William A. Kennington III20db3bf2018-05-14 16:18:37 -070082void SdBusError::populateMessage(const char* prefix)
83{
84 full_message = prefix;
85 if (error.name)
86 {
87 full_message += ": ";
88 full_message += error.name;
89 }
90 if (error.message)
91 {
92 full_message += ": ";
93 full_message += error.message;
94 }
95}
96
97void SdBusError::move(SdBusError&& other)
98{
William A. Kennington III37657502018-06-22 19:00:05 -070099 intf = std::move(other.intf);
100
101 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -0700102 error = other.error;
103 other.error = SD_BUS_ERROR_NULL;
104
105 full_message = std::move(other.full_message);
106}
107
Patrick Williams93b0e702017-04-18 11:19:45 -0500108const char* InvalidEnumString::name() const noexcept
109{
110 return errName;
111}
112
113const char* InvalidEnumString::description() const noexcept
114{
115 return errDesc;
116}
117
118const char* InvalidEnumString::what() const noexcept
119{
120 return errWhat;
121}
122
123} // namespace exception
124} // namespace sdbusplus