Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 1 | #include <sdbusplus/exception.hpp> |
| 2 | |
| 3 | namespace sdbusplus |
| 4 | { |
| 5 | namespace exception |
| 6 | { |
| 7 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame^] | 8 | SdBusError::SdBusError(int error, const char* prefix) : |
| 9 | std::system_error(error, std::generic_category()), error(SD_BUS_ERROR_NULL) |
| 10 | { |
| 11 | if (error == ENOMEM || |
| 12 | sd_bus_error_set_errno(&this->error, error) == -ENOMEM) |
| 13 | { |
| 14 | throw std::bad_alloc(); |
| 15 | } |
| 16 | |
| 17 | populateMessage(prefix); |
| 18 | } |
| 19 | |
| 20 | SdBusError::SdBusError(sd_bus_error error, const char* prefix) : |
| 21 | std::system_error(sd_bus_error_get_errno(&error), std::generic_category()), |
| 22 | error(error) |
| 23 | { |
| 24 | populateMessage(prefix); |
| 25 | } |
| 26 | |
| 27 | SdBusError::SdBusError(SdBusError&& other) |
| 28 | { |
| 29 | move(std::move(other)); |
| 30 | } |
| 31 | |
| 32 | SdBusError& SdBusError::operator=(SdBusError&& other) |
| 33 | { |
| 34 | if (this != &other) |
| 35 | { |
| 36 | move(std::move(other)); |
| 37 | } |
| 38 | return *this; |
| 39 | } |
| 40 | |
| 41 | SdBusError::~SdBusError() |
| 42 | { |
| 43 | sd_bus_error_free(&error); |
| 44 | } |
| 45 | |
| 46 | const char* SdBusError::name() const noexcept |
| 47 | { |
| 48 | return error.name; |
| 49 | } |
| 50 | |
| 51 | const char* SdBusError::description() const noexcept |
| 52 | { |
| 53 | return error.message; |
| 54 | } |
| 55 | |
| 56 | const char* SdBusError::what() const noexcept |
| 57 | { |
| 58 | return full_message.c_str(); |
| 59 | } |
| 60 | |
| 61 | void SdBusError::populateMessage(const char* prefix) |
| 62 | { |
| 63 | full_message = prefix; |
| 64 | if (error.name) |
| 65 | { |
| 66 | full_message += ": "; |
| 67 | full_message += error.name; |
| 68 | } |
| 69 | if (error.message) |
| 70 | { |
| 71 | full_message += ": "; |
| 72 | full_message += error.message; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void SdBusError::move(SdBusError&& other) |
| 77 | { |
| 78 | sd_bus_error_free(&error); |
| 79 | error = other.error; |
| 80 | other.error = SD_BUS_ERROR_NULL; |
| 81 | |
| 82 | full_message = std::move(other.full_message); |
| 83 | } |
| 84 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 85 | const char* InvalidEnumString::name() const noexcept |
| 86 | { |
| 87 | return errName; |
| 88 | } |
| 89 | |
| 90 | const char* InvalidEnumString::description() const noexcept |
| 91 | { |
| 92 | return errDesc; |
| 93 | } |
| 94 | |
| 95 | const char* InvalidEnumString::what() const noexcept |
| 96 | { |
| 97 | return errWhat; |
| 98 | } |
| 99 | |
| 100 | } // namespace exception |
| 101 | } // namespace sdbusplus |