17f664254SPatrick Williams #include <sdbusplus/exception.hpp> 27f664254SPatrick Williams 3b6f729d6SPatrick 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 17b6f729d6SPatrick Williams int exception::get_errno() const noexcept 18b6f729d6SPatrick Williams { 19b6f729d6SPatrick Williams return EIO; 20b6f729d6SPatrick Williams } 21b6f729d6SPatrick Williams 22*15228663SPatrick Williams int generated_exception::get_errno() const noexcept 23*15228663SPatrick Williams { 24*15228663SPatrick Williams return EIO; 25*15228663SPatrick Williams } 26*15228663SPatrick Williams 277f664254SPatrick Williams SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : 287f664254SPatrick Williams error(SD_BUS_ERROR_NULL), intf(intf) 297f664254SPatrick Williams { 307f664254SPatrick Williams // We can't check the output of intf->sd_bus_error_set_errno() because 317f664254SPatrick Williams // it returns the input errorcode. We don't want to try and guess 327f664254SPatrick Williams // possible error statuses. Instead, check to see if the error was 337f664254SPatrick Williams // constructed to determine success. 347f664254SPatrick Williams intf->sd_bus_error_set_errno(&this->error, error); 357f664254SPatrick Williams if (!intf->sd_bus_error_is_set(&this->error)) 367f664254SPatrick Williams { 377f664254SPatrick Williams throw std::runtime_error("Failed to create SdBusError"); 387f664254SPatrick Williams } 397f664254SPatrick Williams 407f664254SPatrick Williams populateMessage(prefix); 417f664254SPatrick Williams } 427f664254SPatrick Williams 437f664254SPatrick Williams SdBusError::SdBusError(sd_bus_error* error, const char* prefix, 447f664254SPatrick Williams SdBusInterface* intf) : 457f664254SPatrick Williams error(*error), 467f664254SPatrick Williams intf(intf) 477f664254SPatrick Williams { 487f664254SPatrick Williams // We own the error so remove the caller's reference 497f664254SPatrick Williams *error = SD_BUS_ERROR_NULL; 507f664254SPatrick Williams 517f664254SPatrick Williams populateMessage(prefix); 527f664254SPatrick Williams } 537f664254SPatrick Williams 547f664254SPatrick Williams SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) 557f664254SPatrick Williams { 567f664254SPatrick Williams move(std::move(other)); 577f664254SPatrick Williams } 587f664254SPatrick Williams 597f664254SPatrick Williams SdBusError& SdBusError::operator=(SdBusError&& other) 607f664254SPatrick Williams { 617f664254SPatrick Williams if (this != &other) 627f664254SPatrick Williams { 637f664254SPatrick Williams move(std::move(other)); 647f664254SPatrick Williams } 657f664254SPatrick Williams return *this; 667f664254SPatrick Williams } 677f664254SPatrick Williams 687f664254SPatrick Williams SdBusError::~SdBusError() 697f664254SPatrick Williams { 707f664254SPatrick Williams intf->sd_bus_error_free(&error); 717f664254SPatrick Williams } 727f664254SPatrick Williams 737f664254SPatrick Williams const char* SdBusError::name() const noexcept 747f664254SPatrick Williams { 757f664254SPatrick Williams return error.name; 767f664254SPatrick Williams } 777f664254SPatrick Williams 787f664254SPatrick Williams const char* SdBusError::description() const noexcept 797f664254SPatrick Williams { 807f664254SPatrick Williams return error.message; 817f664254SPatrick Williams } 827f664254SPatrick Williams 837f664254SPatrick Williams const char* SdBusError::what() const noexcept 847f664254SPatrick Williams { 857f664254SPatrick Williams return full_message.c_str(); 867f664254SPatrick Williams } 877f664254SPatrick Williams 887f664254SPatrick Williams int SdBusError::get_errno() const noexcept 897f664254SPatrick Williams { 907f664254SPatrick Williams return intf->sd_bus_error_get_errno(&this->error); 917f664254SPatrick Williams } 927f664254SPatrick Williams 937f664254SPatrick Williams const sd_bus_error* SdBusError::get_error() const noexcept 947f664254SPatrick Williams { 957f664254SPatrick Williams return &error; 967f664254SPatrick Williams } 977f664254SPatrick Williams 987f664254SPatrick Williams void SdBusError::populateMessage(const char* prefix) 997f664254SPatrick Williams { 1007f664254SPatrick Williams full_message = prefix; 1017f664254SPatrick Williams if (error.name) 1027f664254SPatrick Williams { 1037f664254SPatrick Williams full_message += ": "; 1047f664254SPatrick Williams full_message += error.name; 1057f664254SPatrick Williams } 1067f664254SPatrick Williams if (error.message) 1077f664254SPatrick Williams { 1087f664254SPatrick Williams full_message += ": "; 1097f664254SPatrick Williams full_message += error.message; 1107f664254SPatrick Williams } 1117f664254SPatrick Williams } 1127f664254SPatrick Williams 1137f664254SPatrick Williams void SdBusError::move(SdBusError&& other) 1147f664254SPatrick Williams { 1157f664254SPatrick Williams intf = std::move(other.intf); 1167f664254SPatrick Williams 1177f664254SPatrick Williams intf->sd_bus_error_free(&error); 1187f664254SPatrick Williams error = other.error; 1197f664254SPatrick Williams other.error = SD_BUS_ERROR_NULL; 1207f664254SPatrick Williams 1217f664254SPatrick Williams full_message = std::move(other.full_message); 1227f664254SPatrick Williams } 1237f664254SPatrick Williams 1247f664254SPatrick Williams const char* InvalidEnumString::name() const noexcept 1257f664254SPatrick Williams { 1267f664254SPatrick Williams return errName; 1277f664254SPatrick Williams } 1287f664254SPatrick Williams 1297f664254SPatrick Williams const char* InvalidEnumString::description() const noexcept 1307f664254SPatrick Williams { 1317f664254SPatrick Williams return errDesc; 1327f664254SPatrick Williams } 1337f664254SPatrick Williams 1347f664254SPatrick Williams const char* InvalidEnumString::what() const noexcept 1357f664254SPatrick Williams { 1367f664254SPatrick Williams return errWhat; 1377f664254SPatrick Williams } 1387f664254SPatrick Williams 139bd372ecbSPatrick Williams int InvalidEnumString::get_errno() const noexcept 140bd372ecbSPatrick Williams { 141bd372ecbSPatrick Williams return EINVAL; 142bd372ecbSPatrick Williams } 143bd372ecbSPatrick Williams 14409b88f26SKrzysztof Grobelny UnpackPropertyError::UnpackPropertyError(std::string_view propertyName, 14509b88f26SKrzysztof Grobelny std::string_view reason) : 14609b88f26SKrzysztof Grobelny propertyName(propertyName), 14709b88f26SKrzysztof Grobelny reason(reason) 14809b88f26SKrzysztof Grobelny {} 14909b88f26SKrzysztof Grobelny 15009b88f26SKrzysztof Grobelny const char* UnpackPropertyError::name() const noexcept 15109b88f26SKrzysztof Grobelny { 15209b88f26SKrzysztof Grobelny return errName; 15309b88f26SKrzysztof Grobelny } 15409b88f26SKrzysztof Grobelny 15509b88f26SKrzysztof Grobelny const char* UnpackPropertyError::description() const noexcept 15609b88f26SKrzysztof Grobelny { 15709b88f26SKrzysztof Grobelny return errDesc; 15809b88f26SKrzysztof Grobelny } 15909b88f26SKrzysztof Grobelny 16009b88f26SKrzysztof Grobelny const char* UnpackPropertyError::what() const noexcept 16109b88f26SKrzysztof Grobelny { 16209b88f26SKrzysztof Grobelny return errWhat; 16309b88f26SKrzysztof Grobelny } 16409b88f26SKrzysztof Grobelny 165bd372ecbSPatrick Williams int UnpackPropertyError::get_errno() const noexcept 166bd372ecbSPatrick Williams { 167bd372ecbSPatrick Williams return EINVAL; 168bd372ecbSPatrick Williams } 169bd372ecbSPatrick Williams 1707f664254SPatrick Williams } // namespace exception 1717f664254SPatrick Williams } // namespace sdbusplus 1720c8136a4SPatrick Williams 1730c8136a4SPatrick Williams #ifdef __clang__ 1740c8136a4SPatrick Williams #pragma clang diagnostic pop 1750c8136a4SPatrick Williams #endif 176