xref: /openbmc/sdbusplus/src/exception.cpp (revision b6f729d68d9edcd43fa236eee386b193a95588e7)
17f664254SPatrick Williams #include <sdbusplus/exception.hpp>
27f664254SPatrick Williams 
3*b6f729d6SPatrick Williams #include <cerrno>
47f664254SPatrick Williams #include <stdexcept>
57f664254SPatrick Williams #include <utility>
67f664254SPatrick Williams 
70c8136a4SPatrick Williams #ifdef __clang__
80c8136a4SPatrick Williams #pragma clang diagnostic push
90c8136a4SPatrick Williams #pragma clang diagnostic ignored "-Wc99-extensions"
100c8136a4SPatrick Williams #endif
110c8136a4SPatrick Williams 
127f664254SPatrick Williams namespace sdbusplus
137f664254SPatrick Williams {
147f664254SPatrick Williams namespace exception
157f664254SPatrick Williams {
167f664254SPatrick Williams 
17*b6f729d6SPatrick Williams int exception::get_errno() const noexcept
18*b6f729d6SPatrick Williams {
19*b6f729d6SPatrick Williams     return EIO;
20*b6f729d6SPatrick Williams }
21*b6f729d6SPatrick Williams 
227f664254SPatrick Williams SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) :
237f664254SPatrick Williams     error(SD_BUS_ERROR_NULL), intf(intf)
247f664254SPatrick Williams {
257f664254SPatrick Williams     // We can't check the output of intf->sd_bus_error_set_errno() because
267f664254SPatrick Williams     // it returns the input errorcode. We don't want to try and guess
277f664254SPatrick Williams     // possible error statuses. Instead, check to see if the error was
287f664254SPatrick Williams     // constructed to determine success.
297f664254SPatrick Williams     intf->sd_bus_error_set_errno(&this->error, error);
307f664254SPatrick Williams     if (!intf->sd_bus_error_is_set(&this->error))
317f664254SPatrick Williams     {
327f664254SPatrick Williams         throw std::runtime_error("Failed to create SdBusError");
337f664254SPatrick Williams     }
347f664254SPatrick Williams 
357f664254SPatrick Williams     populateMessage(prefix);
367f664254SPatrick Williams }
377f664254SPatrick Williams 
387f664254SPatrick Williams SdBusError::SdBusError(sd_bus_error* error, const char* prefix,
397f664254SPatrick Williams                        SdBusInterface* intf) :
407f664254SPatrick Williams     error(*error),
417f664254SPatrick Williams     intf(intf)
427f664254SPatrick Williams {
437f664254SPatrick Williams     // We own the error so remove the caller's reference
447f664254SPatrick Williams     *error = SD_BUS_ERROR_NULL;
457f664254SPatrick Williams 
467f664254SPatrick Williams     populateMessage(prefix);
477f664254SPatrick Williams }
487f664254SPatrick Williams 
497f664254SPatrick Williams SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
507f664254SPatrick Williams {
517f664254SPatrick Williams     move(std::move(other));
527f664254SPatrick Williams }
537f664254SPatrick Williams 
547f664254SPatrick Williams SdBusError& SdBusError::operator=(SdBusError&& other)
557f664254SPatrick Williams {
567f664254SPatrick Williams     if (this != &other)
577f664254SPatrick Williams     {
587f664254SPatrick Williams         move(std::move(other));
597f664254SPatrick Williams     }
607f664254SPatrick Williams     return *this;
617f664254SPatrick Williams }
627f664254SPatrick Williams 
637f664254SPatrick Williams SdBusError::~SdBusError()
647f664254SPatrick Williams {
657f664254SPatrick Williams     intf->sd_bus_error_free(&error);
667f664254SPatrick Williams }
677f664254SPatrick Williams 
687f664254SPatrick Williams const char* SdBusError::name() const noexcept
697f664254SPatrick Williams {
707f664254SPatrick Williams     return error.name;
717f664254SPatrick Williams }
727f664254SPatrick Williams 
737f664254SPatrick Williams const char* SdBusError::description() const noexcept
747f664254SPatrick Williams {
757f664254SPatrick Williams     return error.message;
767f664254SPatrick Williams }
777f664254SPatrick Williams 
787f664254SPatrick Williams const char* SdBusError::what() const noexcept
797f664254SPatrick Williams {
807f664254SPatrick Williams     return full_message.c_str();
817f664254SPatrick Williams }
827f664254SPatrick Williams 
837f664254SPatrick Williams int SdBusError::get_errno() const noexcept
847f664254SPatrick Williams {
857f664254SPatrick Williams     return intf->sd_bus_error_get_errno(&this->error);
867f664254SPatrick Williams }
877f664254SPatrick Williams 
887f664254SPatrick Williams const sd_bus_error* SdBusError::get_error() const noexcept
897f664254SPatrick Williams {
907f664254SPatrick Williams     return &error;
917f664254SPatrick Williams }
927f664254SPatrick Williams 
937f664254SPatrick Williams void SdBusError::populateMessage(const char* prefix)
947f664254SPatrick Williams {
957f664254SPatrick Williams     full_message = prefix;
967f664254SPatrick Williams     if (error.name)
977f664254SPatrick Williams     {
987f664254SPatrick Williams         full_message += ": ";
997f664254SPatrick Williams         full_message += error.name;
1007f664254SPatrick Williams     }
1017f664254SPatrick Williams     if (error.message)
1027f664254SPatrick Williams     {
1037f664254SPatrick Williams         full_message += ": ";
1047f664254SPatrick Williams         full_message += error.message;
1057f664254SPatrick Williams     }
1067f664254SPatrick Williams }
1077f664254SPatrick Williams 
1087f664254SPatrick Williams void SdBusError::move(SdBusError&& other)
1097f664254SPatrick Williams {
1107f664254SPatrick Williams     intf = std::move(other.intf);
1117f664254SPatrick Williams 
1127f664254SPatrick Williams     intf->sd_bus_error_free(&error);
1137f664254SPatrick Williams     error = other.error;
1147f664254SPatrick Williams     other.error = SD_BUS_ERROR_NULL;
1157f664254SPatrick Williams 
1167f664254SPatrick Williams     full_message = std::move(other.full_message);
1177f664254SPatrick Williams }
1187f664254SPatrick Williams 
1197f664254SPatrick Williams const char* InvalidEnumString::name() const noexcept
1207f664254SPatrick Williams {
1217f664254SPatrick Williams     return errName;
1227f664254SPatrick Williams }
1237f664254SPatrick Williams 
1247f664254SPatrick Williams const char* InvalidEnumString::description() const noexcept
1257f664254SPatrick Williams {
1267f664254SPatrick Williams     return errDesc;
1277f664254SPatrick Williams }
1287f664254SPatrick Williams 
1297f664254SPatrick Williams const char* InvalidEnumString::what() const noexcept
1307f664254SPatrick Williams {
1317f664254SPatrick Williams     return errWhat;
1327f664254SPatrick Williams }
1337f664254SPatrick Williams 
13409b88f26SKrzysztof Grobelny UnpackPropertyError::UnpackPropertyError(std::string_view propertyName,
13509b88f26SKrzysztof Grobelny                                          std::string_view reason) :
13609b88f26SKrzysztof Grobelny     propertyName(propertyName),
13709b88f26SKrzysztof Grobelny     reason(reason)
13809b88f26SKrzysztof Grobelny {}
13909b88f26SKrzysztof Grobelny 
14009b88f26SKrzysztof Grobelny const char* UnpackPropertyError::name() const noexcept
14109b88f26SKrzysztof Grobelny {
14209b88f26SKrzysztof Grobelny     return errName;
14309b88f26SKrzysztof Grobelny }
14409b88f26SKrzysztof Grobelny 
14509b88f26SKrzysztof Grobelny const char* UnpackPropertyError::description() const noexcept
14609b88f26SKrzysztof Grobelny {
14709b88f26SKrzysztof Grobelny     return errDesc;
14809b88f26SKrzysztof Grobelny }
14909b88f26SKrzysztof Grobelny 
15009b88f26SKrzysztof Grobelny const char* UnpackPropertyError::what() const noexcept
15109b88f26SKrzysztof Grobelny {
15209b88f26SKrzysztof Grobelny     return errWhat;
15309b88f26SKrzysztof Grobelny }
15409b88f26SKrzysztof Grobelny 
1557f664254SPatrick Williams } // namespace exception
1567f664254SPatrick Williams } // namespace sdbusplus
1570c8136a4SPatrick Williams 
1580c8136a4SPatrick Williams #ifdef __clang__
1590c8136a4SPatrick Williams #pragma clang diagnostic pop
1600c8136a4SPatrick Williams #endif
161