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