xref: /openbmc/sdbusplus/src/exception.cpp (revision ef26221b)
1 #include <sdbusplus/exception.hpp>
2 
3 #include <cerrno>
4 #include <stdexcept>
5 #include <utility>
6 
7 #ifdef __clang__
8 #pragma clang diagnostic push
9 #pragma clang diagnostic ignored "-Wc99-extensions"
10 #endif
11 
12 namespace sdbusplus
13 {
14 namespace exception
15 {
16 
17 int exception::get_errno() const noexcept
18 {
19     return EIO;
20 }
21 
22 int generated_exception::get_errno() const noexcept
23 {
24     return EIO;
25 }
26 
27 SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
28     error(SD_BUS_ERROR_NULL), intf(intf)
29 {
30     // We can't check the output of intf->sd_bus_error_set_errno() because
31     // it returns the input errorcode. We don't want to try and guess
32     // possible error statuses. Instead, check to see if the error was
33     // constructed to determine success.
34     intf->sd_bus_error_set_errno(&this->error, error);
35     if (!intf->sd_bus_error_is_set(&this->error))
36     {
37         throw std::runtime_error("Failed to create SdBusError");
38     }
39 
40     populateMessage(prefix);
41 }
42 
43 SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
44                        SdBusInterface* intf) :
45     error(*error),
46     intf(intf)
47 {
48     // We own the error so remove the caller's reference
49     *error = SD_BUS_ERROR_NULL;
50 
51     populateMessage(prefix);
52 }
53 
54 SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
55 {
56     move(std::move(other));
57 }
58 
59 SdBusError& SdBusError::operator=(SdBusError&& other)
60 {
61     if (this != &other)
62     {
63         move(std::move(other));
64     }
65     return *this;
66 }
67 
68 SdBusError::~SdBusError()
69 {
70     intf->sd_bus_error_free(&error);
71 }
72 
73 const char* SdBusError::name() const noexcept
74 {
75     return error.name;
76 }
77 
78 const char* SdBusError::description() const noexcept
79 {
80     return error.message;
81 }
82 
83 const char* SdBusError::what() const noexcept
84 {
85     return full_message.c_str();
86 }
87 
88 int SdBusError::get_errno() const noexcept
89 {
90     return intf->sd_bus_error_get_errno(&this->error);
91 }
92 
93 const sd_bus_error* SdBusError::get_error() const noexcept
94 {
95     return &error;
96 }
97 
98 void SdBusError::populateMessage(const char* prefix)
99 {
100     full_message = prefix;
101     if (error.name)
102     {
103         full_message += ": ";
104         full_message += error.name;
105     }
106     if (error.message)
107     {
108         full_message += ": ";
109         full_message += error.message;
110     }
111 }
112 
113 void SdBusError::move(SdBusError&& other)
114 {
115     intf = std::move(other.intf);
116 
117     intf->sd_bus_error_free(&error);
118     error = other.error;
119     other.error = SD_BUS_ERROR_NULL;
120 
121     full_message = std::move(other.full_message);
122 }
123 
124 const char* InvalidEnumString::name() const noexcept
125 {
126     return errName;
127 }
128 
129 const char* InvalidEnumString::description() const noexcept
130 {
131     return errDesc;
132 }
133 
134 const char* InvalidEnumString::what() const noexcept
135 {
136     return errWhat;
137 }
138 
139 int InvalidEnumString::get_errno() const noexcept
140 {
141     return EINVAL;
142 }
143 
144 UnpackPropertyError::UnpackPropertyError(std::string_view propertyName,
145                                          std::string_view reason) :
146     propertyName(propertyName),
147     reason(reason)
148 {}
149 
150 const char* UnpackPropertyError::name() const noexcept
151 {
152     return errName;
153 }
154 
155 const char* UnpackPropertyError::description() const noexcept
156 {
157     return errDesc;
158 }
159 
160 const char* UnpackPropertyError::what() const noexcept
161 {
162     return errWhat;
163 }
164 
165 int UnpackPropertyError::get_errno() const noexcept
166 {
167     return EINVAL;
168 }
169 
170 } // namespace exception
171 } // namespace sdbusplus
172 
173 #ifdef __clang__
174 #pragma clang diagnostic pop
175 #endif
176