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 | f289c65 | 2022-08-26 10:43:10 -0500 | [diff] [blame] | 17 | void exception::unused() const noexcept {} |
Patrick Williams | a8e3502 | 2021-09-28 06:16:52 -0500 | [diff] [blame] | 18 | |
Patrick Williams | 1522866 | 2021-09-03 06:04:25 -0500 | [diff] [blame] | 19 | int generated_exception::get_errno() const noexcept |
| 20 | { |
| 21 | return EIO; |
| 22 | } |
| 23 | |
Ed Tanous | ed4a5a6 | 2023-01-05 09:38:50 -0800 | [diff] [blame] | 24 | SdBusError::SdBusError(int error_in, const char* prefix, |
| 25 | SdBusInterface* intf_in) : |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 26 | SdBusError(error_in, std::string(prefix), intf_in) |
| 27 | {} |
| 28 | |
| 29 | SdBusError::SdBusError(int error_in, std::string&& prefix, |
| 30 | SdBusInterface* intf_in) : |
Ed Tanous | ed4a5a6 | 2023-01-05 09:38:50 -0800 | [diff] [blame] | 31 | error(SD_BUS_ERROR_NULL), |
| 32 | intf(intf_in) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 33 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 34 | // We can't check the output of intf->sd_bus_error_set_errno() because |
| 35 | // it returns the input errorcode. We don't want to try and guess |
| 36 | // possible error statuses. Instead, check to see if the error was |
| 37 | // constructed to determine success. |
Ed Tanous | ed4a5a6 | 2023-01-05 09:38:50 -0800 | [diff] [blame] | 38 | intf->sd_bus_error_set_errno(&this->error, error_in); |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 39 | if (!intf->sd_bus_error_is_set(&this->error)) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 40 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 41 | throw std::runtime_error("Failed to create SdBusError"); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 44 | populateMessage(std::move(prefix)); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ed Tanous | ed4a5a6 | 2023-01-05 09:38:50 -0800 | [diff] [blame] | 47 | SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix, |
| 48 | SdBusInterface* intf_in) : |
| 49 | error(*error_in), |
| 50 | intf(intf_in) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 51 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 52 | // We own the error so remove the caller's reference |
Ed Tanous | ed4a5a6 | 2023-01-05 09:38:50 -0800 | [diff] [blame] | 53 | *error_in = SD_BUS_ERROR_NULL; |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 54 | |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 55 | populateMessage(std::string(prefix)); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 58 | SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 59 | { |
| 60 | move(std::move(other)); |
| 61 | } |
| 62 | |
| 63 | SdBusError& SdBusError::operator=(SdBusError&& other) |
| 64 | { |
| 65 | if (this != &other) |
| 66 | { |
| 67 | move(std::move(other)); |
| 68 | } |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | SdBusError::~SdBusError() |
| 73 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 74 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const char* SdBusError::name() const noexcept |
| 78 | { |
| 79 | return error.name; |
| 80 | } |
| 81 | |
| 82 | const char* SdBusError::description() const noexcept |
| 83 | { |
| 84 | return error.message; |
| 85 | } |
| 86 | |
| 87 | const char* SdBusError::what() const noexcept |
| 88 | { |
| 89 | return full_message.c_str(); |
| 90 | } |
| 91 | |
Adriana Kobylak | 33335b3 | 2018-09-21 11:50:42 -0500 | [diff] [blame] | 92 | int SdBusError::get_errno() const noexcept |
| 93 | { |
| 94 | return intf->sd_bus_error_get_errno(&this->error); |
| 95 | } |
| 96 | |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 97 | const sd_bus_error* SdBusError::get_error() const noexcept |
| 98 | { |
| 99 | return &error; |
| 100 | } |
| 101 | |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 102 | void SdBusError::populateMessage(std::string&& prefix) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 103 | { |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 104 | full_message = std::move(prefix); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 105 | if (error.name) |
| 106 | { |
| 107 | full_message += ": "; |
| 108 | full_message += error.name; |
| 109 | } |
| 110 | if (error.message) |
| 111 | { |
| 112 | full_message += ": "; |
| 113 | full_message += error.message; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void SdBusError::move(SdBusError&& other) |
| 118 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 119 | intf = std::move(other.intf); |
| 120 | |
| 121 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 122 | error = other.error; |
| 123 | other.error = SD_BUS_ERROR_NULL; |
| 124 | |
| 125 | full_message = std::move(other.full_message); |
| 126 | } |
| 127 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 128 | const char* InvalidEnumString::name() const noexcept |
| 129 | { |
| 130 | return errName; |
| 131 | } |
| 132 | |
| 133 | const char* InvalidEnumString::description() const noexcept |
| 134 | { |
| 135 | return errDesc; |
| 136 | } |
| 137 | |
| 138 | const char* InvalidEnumString::what() const noexcept |
| 139 | { |
| 140 | return errWhat; |
| 141 | } |
| 142 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 143 | int InvalidEnumString::get_errno() const noexcept |
| 144 | { |
| 145 | return EINVAL; |
| 146 | } |
| 147 | |
Ed Tanous | c348473 | 2023-01-04 15:59:18 -0800 | [diff] [blame] | 148 | static std::string unpackErrorReasonToString(const UnpackErrorReason reason) |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 149 | { |
| 150 | switch (reason) |
| 151 | { |
| 152 | case UnpackErrorReason::missingProperty: |
| 153 | return "Missing property"; |
| 154 | case UnpackErrorReason::wrongType: |
| 155 | return "Type not matched"; |
| 156 | } |
| 157 | return "Unknown"; |
| 158 | } |
| 159 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 160 | UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 161 | const UnpackErrorReason reasonIn) : |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 162 | propertyName(propertyNameIn), |
| 163 | reason(reasonIn), |
| 164 | errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName + |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 165 | "', Reason: '" + unpackErrorReasonToString(reason) + "'.") |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 166 | {} |
| 167 | |
| 168 | const char* UnpackPropertyError::name() const noexcept |
| 169 | { |
| 170 | return errName; |
| 171 | } |
| 172 | |
| 173 | const char* UnpackPropertyError::description() const noexcept |
| 174 | { |
| 175 | return errDesc; |
| 176 | } |
| 177 | |
| 178 | const char* UnpackPropertyError::what() const noexcept |
| 179 | { |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 180 | return errWhatDetailed.c_str(); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 183 | int UnpackPropertyError::get_errno() const noexcept |
| 184 | { |
| 185 | return EINVAL; |
| 186 | } |
| 187 | |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 188 | const char* UnhandledStop::name() const noexcept |
| 189 | { |
| 190 | return errName; |
| 191 | } |
| 192 | |
| 193 | const char* UnhandledStop::description() const noexcept |
| 194 | { |
| 195 | return errDesc; |
| 196 | } |
| 197 | |
| 198 | const char* UnhandledStop::what() const noexcept |
| 199 | { |
| 200 | return errWhat; |
| 201 | } |
| 202 | |
| 203 | int UnhandledStop::get_errno() const noexcept |
| 204 | { |
| 205 | return ECANCELED; |
| 206 | } |
| 207 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 208 | } // namespace exception |
| 209 | } // namespace sdbusplus |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 210 | |
| 211 | #ifdef __clang__ |
| 212 | #pragma clang diagnostic pop |
| 213 | #endif |