Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 1 | #include <sdbusplus/exception.hpp> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 2 | #include <stdexcept> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 3 | #include <utility> |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 4 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 5 | namespace sdbusplus |
| 6 | { |
| 7 | namespace exception |
| 8 | { |
| 9 | |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 10 | SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 11 | error(SD_BUS_ERROR_NULL), intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 12 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 13 | // We can't check the output of intf->sd_bus_error_set_errno() because |
| 14 | // it returns the input errorcode. We don't want to try and guess |
| 15 | // possible error statuses. Instead, check to see if the error was |
| 16 | // constructed to determine success. |
| 17 | intf->sd_bus_error_set_errno(&this->error, error); |
| 18 | if (!intf->sd_bus_error_is_set(&this->error)) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 19 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 20 | throw std::runtime_error("Failed to create SdBusError"); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | populateMessage(prefix); |
| 24 | } |
| 25 | |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 26 | SdBusError::SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 27 | SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 28 | error(*error), |
| 29 | intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 30 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 31 | // We own the error so remove the caller's reference |
| 32 | *error = SD_BUS_ERROR_NULL; |
| 33 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 34 | populateMessage(prefix); |
| 35 | } |
| 36 | |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 37 | SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 38 | { |
| 39 | move(std::move(other)); |
| 40 | } |
| 41 | |
| 42 | SdBusError& SdBusError::operator=(SdBusError&& other) |
| 43 | { |
| 44 | if (this != &other) |
| 45 | { |
| 46 | move(std::move(other)); |
| 47 | } |
| 48 | return *this; |
| 49 | } |
| 50 | |
| 51 | SdBusError::~SdBusError() |
| 52 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 53 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | const char* SdBusError::name() const noexcept |
| 57 | { |
| 58 | return error.name; |
| 59 | } |
| 60 | |
| 61 | const char* SdBusError::description() const noexcept |
| 62 | { |
| 63 | return error.message; |
| 64 | } |
| 65 | |
| 66 | const char* SdBusError::what() const noexcept |
| 67 | { |
| 68 | return full_message.c_str(); |
| 69 | } |
| 70 | |
Adriana Kobylak | 33335b3 | 2018-09-21 11:50:42 -0500 | [diff] [blame] | 71 | int SdBusError::get_errno() const noexcept |
| 72 | { |
| 73 | return intf->sd_bus_error_get_errno(&this->error); |
| 74 | } |
| 75 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 76 | void SdBusError::populateMessage(const char* prefix) |
| 77 | { |
| 78 | full_message = prefix; |
| 79 | if (error.name) |
| 80 | { |
| 81 | full_message += ": "; |
| 82 | full_message += error.name; |
| 83 | } |
| 84 | if (error.message) |
| 85 | { |
| 86 | full_message += ": "; |
| 87 | full_message += error.message; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void SdBusError::move(SdBusError&& other) |
| 92 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 93 | intf = std::move(other.intf); |
| 94 | |
| 95 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 96 | error = other.error; |
| 97 | other.error = SD_BUS_ERROR_NULL; |
| 98 | |
| 99 | full_message = std::move(other.full_message); |
| 100 | } |
| 101 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 102 | const char* InvalidEnumString::name() const noexcept |
| 103 | { |
| 104 | return errName; |
| 105 | } |
| 106 | |
| 107 | const char* InvalidEnumString::description() const noexcept |
| 108 | { |
| 109 | return errDesc; |
| 110 | } |
| 111 | |
| 112 | const char* InvalidEnumString::what() const noexcept |
| 113 | { |
| 114 | return errWhat; |
| 115 | } |
| 116 | |
| 117 | } // namespace exception |
| 118 | } // namespace sdbusplus |