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