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