17f664254SPatrick Williams #include <sdbusplus/exception.hpp> 27f664254SPatrick Williams 37f664254SPatrick Williams #include <stdexcept> 47f664254SPatrick Williams #include <utility> 57f664254SPatrick Williams 67f664254SPatrick Williams namespace sdbusplus 77f664254SPatrick Williams { 87f664254SPatrick Williams namespace exception 97f664254SPatrick Williams { 107f664254SPatrick Williams 117f664254SPatrick Williams SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : 127f664254SPatrick Williams error(SD_BUS_ERROR_NULL), intf(intf) 137f664254SPatrick Williams { 147f664254SPatrick Williams // We can't check the output of intf->sd_bus_error_set_errno() because 157f664254SPatrick Williams // it returns the input errorcode. We don't want to try and guess 167f664254SPatrick Williams // possible error statuses. Instead, check to see if the error was 177f664254SPatrick Williams // constructed to determine success. 187f664254SPatrick Williams intf->sd_bus_error_set_errno(&this->error, error); 197f664254SPatrick Williams if (!intf->sd_bus_error_is_set(&this->error)) 207f664254SPatrick Williams { 217f664254SPatrick Williams throw std::runtime_error("Failed to create SdBusError"); 227f664254SPatrick Williams } 237f664254SPatrick Williams 247f664254SPatrick Williams populateMessage(prefix); 257f664254SPatrick Williams } 267f664254SPatrick Williams 277f664254SPatrick Williams SdBusError::SdBusError(sd_bus_error* error, const char* prefix, 287f664254SPatrick Williams SdBusInterface* intf) : 297f664254SPatrick Williams error(*error), 307f664254SPatrick Williams intf(intf) 317f664254SPatrick Williams { 327f664254SPatrick Williams // We own the error so remove the caller's reference 337f664254SPatrick Williams *error = SD_BUS_ERROR_NULL; 347f664254SPatrick Williams 357f664254SPatrick Williams populateMessage(prefix); 367f664254SPatrick Williams } 377f664254SPatrick Williams 387f664254SPatrick Williams SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) 397f664254SPatrick Williams { 407f664254SPatrick Williams move(std::move(other)); 417f664254SPatrick Williams } 427f664254SPatrick Williams 437f664254SPatrick Williams SdBusError& SdBusError::operator=(SdBusError&& other) 447f664254SPatrick Williams { 457f664254SPatrick Williams if (this != &other) 467f664254SPatrick Williams { 477f664254SPatrick Williams move(std::move(other)); 487f664254SPatrick Williams } 497f664254SPatrick Williams return *this; 507f664254SPatrick Williams } 517f664254SPatrick Williams 527f664254SPatrick Williams SdBusError::~SdBusError() 537f664254SPatrick Williams { 547f664254SPatrick Williams intf->sd_bus_error_free(&error); 557f664254SPatrick Williams } 567f664254SPatrick Williams 577f664254SPatrick Williams const char* SdBusError::name() const noexcept 587f664254SPatrick Williams { 597f664254SPatrick Williams return error.name; 607f664254SPatrick Williams } 617f664254SPatrick Williams 627f664254SPatrick Williams const char* SdBusError::description() const noexcept 637f664254SPatrick Williams { 647f664254SPatrick Williams return error.message; 657f664254SPatrick Williams } 667f664254SPatrick Williams 677f664254SPatrick Williams const char* SdBusError::what() const noexcept 687f664254SPatrick Williams { 697f664254SPatrick Williams return full_message.c_str(); 707f664254SPatrick Williams } 717f664254SPatrick Williams 727f664254SPatrick Williams int SdBusError::get_errno() const noexcept 737f664254SPatrick Williams { 747f664254SPatrick Williams return intf->sd_bus_error_get_errno(&this->error); 757f664254SPatrick Williams } 767f664254SPatrick Williams 777f664254SPatrick Williams const sd_bus_error* SdBusError::get_error() const noexcept 787f664254SPatrick Williams { 797f664254SPatrick Williams return &error; 807f664254SPatrick Williams } 817f664254SPatrick Williams 827f664254SPatrick Williams void SdBusError::populateMessage(const char* prefix) 837f664254SPatrick Williams { 847f664254SPatrick Williams full_message = prefix; 857f664254SPatrick Williams if (error.name) 867f664254SPatrick Williams { 877f664254SPatrick Williams full_message += ": "; 887f664254SPatrick Williams full_message += error.name; 897f664254SPatrick Williams } 907f664254SPatrick Williams if (error.message) 917f664254SPatrick Williams { 927f664254SPatrick Williams full_message += ": "; 937f664254SPatrick Williams full_message += error.message; 947f664254SPatrick Williams } 957f664254SPatrick Williams } 967f664254SPatrick Williams 977f664254SPatrick Williams void SdBusError::move(SdBusError&& other) 987f664254SPatrick Williams { 997f664254SPatrick Williams intf = std::move(other.intf); 1007f664254SPatrick Williams 1017f664254SPatrick Williams intf->sd_bus_error_free(&error); 1027f664254SPatrick Williams error = other.error; 1037f664254SPatrick Williams other.error = SD_BUS_ERROR_NULL; 1047f664254SPatrick Williams 1057f664254SPatrick Williams full_message = std::move(other.full_message); 1067f664254SPatrick Williams } 1077f664254SPatrick Williams 1087f664254SPatrick Williams const char* InvalidEnumString::name() const noexcept 1097f664254SPatrick Williams { 1107f664254SPatrick Williams return errName; 1117f664254SPatrick Williams } 1127f664254SPatrick Williams 1137f664254SPatrick Williams const char* InvalidEnumString::description() const noexcept 1147f664254SPatrick Williams { 1157f664254SPatrick Williams return errDesc; 1167f664254SPatrick Williams } 1177f664254SPatrick Williams 1187f664254SPatrick Williams const char* InvalidEnumString::what() const noexcept 1197f664254SPatrick Williams { 1207f664254SPatrick Williams return errWhat; 1217f664254SPatrick Williams } 1227f664254SPatrick Williams 123*09b88f26SKrzysztof Grobelny UnpackPropertyError::UnpackPropertyError(std::string_view propertyName, 124*09b88f26SKrzysztof Grobelny std::string_view reason) : 125*09b88f26SKrzysztof Grobelny propertyName(propertyName), 126*09b88f26SKrzysztof Grobelny reason(reason) 127*09b88f26SKrzysztof Grobelny {} 128*09b88f26SKrzysztof Grobelny 129*09b88f26SKrzysztof Grobelny const char* UnpackPropertyError::name() const noexcept 130*09b88f26SKrzysztof Grobelny { 131*09b88f26SKrzysztof Grobelny return errName; 132*09b88f26SKrzysztof Grobelny } 133*09b88f26SKrzysztof Grobelny 134*09b88f26SKrzysztof Grobelny const char* UnpackPropertyError::description() const noexcept 135*09b88f26SKrzysztof Grobelny { 136*09b88f26SKrzysztof Grobelny return errDesc; 137*09b88f26SKrzysztof Grobelny } 138*09b88f26SKrzysztof Grobelny 139*09b88f26SKrzysztof Grobelny const char* UnpackPropertyError::what() const noexcept 140*09b88f26SKrzysztof Grobelny { 141*09b88f26SKrzysztof Grobelny return errWhat; 142*09b88f26SKrzysztof Grobelny } 143*09b88f26SKrzysztof Grobelny 1447f664254SPatrick Williams } // namespace exception 1457f664254SPatrick Williams } // namespace sdbusplus 146