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 | |
Patrick Williams | b6f729d | 2021-09-02 15:02:19 -0500 | [diff] [blame] | 3 | #include <cerrno> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 4 | #include <stdexcept> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 5 | #include <utility> |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 6 | |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 7 | #ifdef __clang__ |
| 8 | #pragma clang diagnostic push |
| 9 | #pragma clang diagnostic ignored "-Wc99-extensions" |
| 10 | #endif |
| 11 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 12 | namespace sdbusplus |
| 13 | { |
| 14 | namespace exception |
| 15 | { |
| 16 | |
Patrick Williams | b6f729d | 2021-09-02 15:02:19 -0500 | [diff] [blame] | 17 | int exception::get_errno() const noexcept |
| 18 | { |
| 19 | return EIO; |
| 20 | } |
| 21 | |
Patrick Williams | 1522866 | 2021-09-03 06:04:25 -0500 | [diff] [blame] | 22 | int generated_exception::get_errno() const noexcept |
| 23 | { |
| 24 | return EIO; |
| 25 | } |
| 26 | |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 27 | SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 28 | error(SD_BUS_ERROR_NULL), intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 29 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 30 | // We can't check the output of intf->sd_bus_error_set_errno() because |
| 31 | // it returns the input errorcode. We don't want to try and guess |
| 32 | // possible error statuses. Instead, check to see if the error was |
| 33 | // constructed to determine success. |
| 34 | intf->sd_bus_error_set_errno(&this->error, error); |
| 35 | if (!intf->sd_bus_error_is_set(&this->error)) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 36 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 37 | throw std::runtime_error("Failed to create SdBusError"); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | populateMessage(prefix); |
| 41 | } |
| 42 | |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 43 | SdBusError::SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 44 | SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 45 | error(*error), |
| 46 | intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 47 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 48 | // We own the error so remove the caller's reference |
| 49 | *error = SD_BUS_ERROR_NULL; |
| 50 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 51 | populateMessage(prefix); |
| 52 | } |
| 53 | |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 54 | SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 55 | { |
| 56 | move(std::move(other)); |
| 57 | } |
| 58 | |
| 59 | SdBusError& SdBusError::operator=(SdBusError&& other) |
| 60 | { |
| 61 | if (this != &other) |
| 62 | { |
| 63 | move(std::move(other)); |
| 64 | } |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | SdBusError::~SdBusError() |
| 69 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 70 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const char* SdBusError::name() const noexcept |
| 74 | { |
| 75 | return error.name; |
| 76 | } |
| 77 | |
| 78 | const char* SdBusError::description() const noexcept |
| 79 | { |
| 80 | return error.message; |
| 81 | } |
| 82 | |
| 83 | const char* SdBusError::what() const noexcept |
| 84 | { |
| 85 | return full_message.c_str(); |
| 86 | } |
| 87 | |
Adriana Kobylak | 33335b3 | 2018-09-21 11:50:42 -0500 | [diff] [blame] | 88 | int SdBusError::get_errno() const noexcept |
| 89 | { |
| 90 | return intf->sd_bus_error_get_errno(&this->error); |
| 91 | } |
| 92 | |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 93 | const sd_bus_error* SdBusError::get_error() const noexcept |
| 94 | { |
| 95 | return &error; |
| 96 | } |
| 97 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 98 | void SdBusError::populateMessage(const char* prefix) |
| 99 | { |
| 100 | full_message = prefix; |
| 101 | if (error.name) |
| 102 | { |
| 103 | full_message += ": "; |
| 104 | full_message += error.name; |
| 105 | } |
| 106 | if (error.message) |
| 107 | { |
| 108 | full_message += ": "; |
| 109 | full_message += error.message; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void SdBusError::move(SdBusError&& other) |
| 114 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 115 | intf = std::move(other.intf); |
| 116 | |
| 117 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 118 | error = other.error; |
| 119 | other.error = SD_BUS_ERROR_NULL; |
| 120 | |
| 121 | full_message = std::move(other.full_message); |
| 122 | } |
| 123 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 124 | const char* InvalidEnumString::name() const noexcept |
| 125 | { |
| 126 | return errName; |
| 127 | } |
| 128 | |
| 129 | const char* InvalidEnumString::description() const noexcept |
| 130 | { |
| 131 | return errDesc; |
| 132 | } |
| 133 | |
| 134 | const char* InvalidEnumString::what() const noexcept |
| 135 | { |
| 136 | return errWhat; |
| 137 | } |
| 138 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 139 | int InvalidEnumString::get_errno() const noexcept |
| 140 | { |
| 141 | return EINVAL; |
| 142 | } |
| 143 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 144 | UnpackPropertyError::UnpackPropertyError(std::string_view propertyName, |
| 145 | std::string_view reason) : |
| 146 | propertyName(propertyName), |
| 147 | reason(reason) |
| 148 | {} |
| 149 | |
| 150 | const char* UnpackPropertyError::name() const noexcept |
| 151 | { |
| 152 | return errName; |
| 153 | } |
| 154 | |
| 155 | const char* UnpackPropertyError::description() const noexcept |
| 156 | { |
| 157 | return errDesc; |
| 158 | } |
| 159 | |
| 160 | const char* UnpackPropertyError::what() const noexcept |
| 161 | { |
| 162 | return errWhat; |
| 163 | } |
| 164 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 165 | int UnpackPropertyError::get_errno() const noexcept |
| 166 | { |
| 167 | return EINVAL; |
| 168 | } |
| 169 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 170 | } // namespace exception |
| 171 | } // namespace sdbusplus |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 172 | |
| 173 | #ifdef __clang__ |
| 174 | #pragma clang diagnostic pop |
| 175 | #endif |