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