blob: 5e99abb77cde4315ac4a6113929465d03cce1c55 [file] [log] [blame]
Patrick Williams93b0e702017-04-18 11:19:45 -05001#include <sdbusplus/exception.hpp>
2
3namespace sdbusplus
4{
5namespace exception
6{
7
William A. Kennington III20db3bf2018-05-14 16:18:37 -07008SdBusError::SdBusError(int error, const char* prefix) :
9 std::system_error(error, std::generic_category()), error(SD_BUS_ERROR_NULL)
10{
11 if (error == ENOMEM ||
12 sd_bus_error_set_errno(&this->error, error) == -ENOMEM)
13 {
14 throw std::bad_alloc();
15 }
16
17 populateMessage(prefix);
18}
19
20SdBusError::SdBusError(sd_bus_error error, const char* prefix) :
21 std::system_error(sd_bus_error_get_errno(&error), std::generic_category()),
22 error(error)
23{
24 populateMessage(prefix);
25}
26
27SdBusError::SdBusError(SdBusError&& other)
28{
29 move(std::move(other));
30}
31
32SdBusError& SdBusError::operator=(SdBusError&& other)
33{
34 if (this != &other)
35 {
36 move(std::move(other));
37 }
38 return *this;
39}
40
41SdBusError::~SdBusError()
42{
43 sd_bus_error_free(&error);
44}
45
46const char* SdBusError::name() const noexcept
47{
48 return error.name;
49}
50
51const char* SdBusError::description() const noexcept
52{
53 return error.message;
54}
55
56const char* SdBusError::what() const noexcept
57{
58 return full_message.c_str();
59}
60
61void SdBusError::populateMessage(const char* prefix)
62{
63 full_message = prefix;
64 if (error.name)
65 {
66 full_message += ": ";
67 full_message += error.name;
68 }
69 if (error.message)
70 {
71 full_message += ": ";
72 full_message += error.message;
73 }
74}
75
76void SdBusError::move(SdBusError&& other)
77{
78 sd_bus_error_free(&error);
79 error = other.error;
80 other.error = SD_BUS_ERROR_NULL;
81
82 full_message = std::move(other.full_message);
83}
84
Patrick Williams93b0e702017-04-18 11:19:45 -050085const char* InvalidEnumString::name() const noexcept
86{
87 return errName;
88}
89
90const char* InvalidEnumString::description() const noexcept
91{
92 return errDesc;
93}
94
95const char* InvalidEnumString::what() const noexcept
96{
97 return errWhat;
98}
99
100} // namespace exception
101} // namespace sdbusplus