blob: a0ecc8dc00846aa0ca082c8eb44cdcfcc9650f83 [file] [log] [blame]
Patrick Williams93b0e702017-04-18 11:19:45 -05001#include <sdbusplus/exception.hpp>
William A. Kennington III2726c172018-06-22 19:39:04 -07002#include <stdexcept>
William A. Kennington III2726c172018-06-22 19:39:04 -07003#include <utility>
William A. Kennington III37657502018-06-22 19:00:05 -07004
Patrick Williams93b0e702017-04-18 11:19:45 -05005namespace sdbusplus
6{
7namespace exception
8{
9
William A. Kennington III37657502018-06-22 19:00:05 -070010SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070011 error(SD_BUS_ERROR_NULL), intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070012{
William A. Kennington III2726c172018-06-22 19:39:04 -070013 // We can't check the output of intf->sd_bus_error_set_errno() because
14 // it returns the input errorcode. We don't want to try and guess
15 // possible error statuses. Instead, check to see if the error was
16 // constructed to determine success.
17 intf->sd_bus_error_set_errno(&this->error, error);
18 if (!intf->sd_bus_error_is_set(&this->error))
William A. Kennington III20db3bf2018-05-14 16:18:37 -070019 {
William A. Kennington III2726c172018-06-22 19:39:04 -070020 throw std::runtime_error("Failed to create SdBusError");
William A. Kennington III20db3bf2018-05-14 16:18:37 -070021 }
22
23 populateMessage(prefix);
24}
25
William A. Kennington III68cb1702018-06-22 19:35:48 -070026SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070027 SdBusInterface* intf) :
Vernon Maueryfac43a62018-08-01 12:29:23 -070028 error(*error),
29 intf(intf)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070030{
William A. Kennington III68cb1702018-06-22 19:35:48 -070031 // We own the error so remove the caller's reference
32 *error = SD_BUS_ERROR_NULL;
33
William A. Kennington III20db3bf2018-05-14 16:18:37 -070034 populateMessage(prefix);
35}
36
Vernon Maueryfac43a62018-08-01 12:29:23 -070037SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
William A. Kennington III20db3bf2018-05-14 16:18:37 -070038{
39 move(std::move(other));
40}
41
42SdBusError& SdBusError::operator=(SdBusError&& other)
43{
44 if (this != &other)
45 {
46 move(std::move(other));
47 }
48 return *this;
49}
50
51SdBusError::~SdBusError()
52{
William A. Kennington III37657502018-06-22 19:00:05 -070053 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070054}
55
56const char* SdBusError::name() const noexcept
57{
58 return error.name;
59}
60
61const char* SdBusError::description() const noexcept
62{
63 return error.message;
64}
65
66const char* SdBusError::what() const noexcept
67{
68 return full_message.c_str();
69}
70
Adriana Kobylak33335b32018-09-21 11:50:42 -050071int SdBusError::get_errno() const noexcept
72{
73 return intf->sd_bus_error_get_errno(&this->error);
74}
75
William A. Kennington III20db3bf2018-05-14 16:18:37 -070076void SdBusError::populateMessage(const char* prefix)
77{
78 full_message = prefix;
79 if (error.name)
80 {
81 full_message += ": ";
82 full_message += error.name;
83 }
84 if (error.message)
85 {
86 full_message += ": ";
87 full_message += error.message;
88 }
89}
90
91void SdBusError::move(SdBusError&& other)
92{
William A. Kennington III37657502018-06-22 19:00:05 -070093 intf = std::move(other.intf);
94
95 intf->sd_bus_error_free(&error);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070096 error = other.error;
97 other.error = SD_BUS_ERROR_NULL;
98
99 full_message = std::move(other.full_message);
100}
101
Patrick Williams93b0e702017-04-18 11:19:45 -0500102const char* InvalidEnumString::name() const noexcept
103{
104 return errName;
105}
106
107const char* InvalidEnumString::description() const noexcept
108{
109 return errDesc;
110}
111
112const char* InvalidEnumString::what() const noexcept
113{
114 return errWhat;
115}
116
117} // namespace exception
118} // namespace sdbusplus