Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 3 | #include <systemd/sd-bus.h> |
| 4 | |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 5 | #include <sdbusplus/sdbus.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 6 | |
| 7 | #include <exception> |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 8 | #include <string> |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 9 | |
| 10 | namespace sdbusplus |
| 11 | { |
| 12 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 13 | enum class UnpackErrorReason |
| 14 | { |
| 15 | missingProperty, |
| 16 | wrongType |
| 17 | }; |
| 18 | |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 19 | namespace exception |
| 20 | { |
| 21 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 22 | /** Base exception class for all sdbusplus exceptions, including those created |
| 23 | * by the bindings. */ |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 24 | struct exception : public std::exception |
| 25 | { |
Patrick Williams | ea24144 | 2016-11-15 14:41:13 -0600 | [diff] [blame] | 26 | virtual const char* name() const noexcept = 0; |
| 27 | virtual const char* description() const noexcept = 0; |
Patrick Williams | b68700e | 2021-09-09 06:05:15 -0500 | [diff] [blame] | 28 | virtual int get_errno() const noexcept = 0; |
Patrick Williams | a8e3502 | 2021-09-28 06:16:52 -0500 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | // This unused function is to ensure that the vtable for this class is |
| 32 | // properly emitted when `-flto=auto` is used, which is the default in |
| 33 | // Yocto builds. Without this, the vtable is a hidden symbol and no |
| 34 | // users can inherit from our exception type directly. |
| 35 | virtual void unused() const noexcept; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 36 | }; |
| 37 | |
Patrick Williams | 1522866 | 2021-09-03 06:04:25 -0500 | [diff] [blame] | 38 | /** base exception class for all errors created by the sdbus++ generator */ |
| 39 | struct generated_exception : public exception |
| 40 | { |
| 41 | int get_errno() const noexcept override; |
| 42 | }; |
| 43 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 44 | /** base exception class for all errors generated by sdbusplus itself. */ |
| 45 | struct internal_exception : public exception |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 46 | {}; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 47 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 48 | /** Exception for when an underlying sd_bus method call fails. */ |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 49 | class SdBusError final : public internal_exception |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
| 52 | /** Errno must be positive */ |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 53 | SdBusError(int error, const char* prefix, |
| 54 | SdBusInterface* intf = &sdbus_impl); |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 55 | SdBusError(int error, std::string&& prefix, |
| 56 | SdBusInterface* intf = &sdbus_impl); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 57 | /** Becomes the owner of the error */ |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 58 | SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 59 | SdBusInterface* intf = &sdbus_impl); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 60 | |
| 61 | SdBusError(const SdBusError&) = delete; |
| 62 | SdBusError& operator=(const SdBusError&) = delete; |
| 63 | SdBusError(SdBusError&& other); |
| 64 | SdBusError& operator=(SdBusError&& other); |
Brad Bishop | 0eda6eb | 2022-09-29 11:04:13 -0400 | [diff] [blame] | 65 | ~SdBusError() override; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 66 | |
| 67 | const char* name() const noexcept override; |
| 68 | const char* description() const noexcept override; |
| 69 | const char* what() const noexcept override; |
Patrick Williams | b6f729d | 2021-09-02 15:02:19 -0500 | [diff] [blame] | 70 | int get_errno() const noexcept override; |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 71 | const sd_bus_error* get_error() const noexcept; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | sd_bus_error error; |
| 75 | std::string full_message; |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 76 | SdBusInterface* intf; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 77 | |
| 78 | /** Populates the full_message from the stored |
| 79 | * error and the passed in prefix. */ |
Patrick Williams | 70bcf14 | 2023-07-26 16:19:48 -0500 | [diff] [blame] | 80 | void populateMessage(std::string&& prefix); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 81 | |
| 82 | /** Helper to reduce duplicate move logic */ |
| 83 | void move(SdBusError&& other); |
| 84 | }; |
| 85 | |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 86 | /** Exception for when an invalid conversion from string to enum is |
| 87 | * attempted. */ |
| 88 | struct InvalidEnumString final : public internal_exception |
| 89 | { |
| 90 | static constexpr auto errName = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 91 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString"; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 92 | static constexpr auto errDesc = |
| 93 | "An enumeration mapping was attempted for which no valid enumeration " |
| 94 | "value exists."; |
| 95 | static constexpr auto errWhat = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 96 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: " |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 97 | "An enumeration mapping was attempted for which no valid enumeration " |
| 98 | "value exists."; |
| 99 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 100 | const char* name() const noexcept override; |
| 101 | const char* description() const noexcept override; |
| 102 | const char* what() const noexcept override; |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 103 | int get_errno() const noexcept override; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 104 | }; |
| 105 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 106 | /** Exception for when unpackProperties cannot find given property in provided |
| 107 | * container */ |
| 108 | class UnpackPropertyError final : public internal_exception |
| 109 | { |
| 110 | public: |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 111 | UnpackPropertyError(std::string_view propertyName, |
| 112 | const UnpackErrorReason reason); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 113 | |
| 114 | static constexpr auto errName = |
| 115 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError"; |
| 116 | static constexpr auto errDesc = |
| 117 | "unpackProperties failed to unpack one of requested properties."; |
| 118 | static constexpr auto errWhat = |
| 119 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: " |
| 120 | "unpackProperties failed to unpack one of requested properties."; |
| 121 | |
| 122 | const char* name() const noexcept override; |
| 123 | const char* description() const noexcept override; |
| 124 | const char* what() const noexcept override; |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 125 | int get_errno() const noexcept override; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 126 | |
| 127 | const std::string propertyName; |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 128 | const UnpackErrorReason reason; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | const std::string errWhatDetailed; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 134 | class UnhandledStop final : public internal_exception |
| 135 | { |
| 136 | public: |
| 137 | static constexpr auto errName = |
| 138 | "xyz.openbmc_project.sdbusplus.Error.UnhandledStop"; |
| 139 | static constexpr auto errDesc = |
| 140 | "An async Sender failed to handle a stop condition."; |
| 141 | static constexpr auto errWhat = |
| 142 | "xyz.openbmc_project.sdbusplus.Error.UnhandledStop: " |
| 143 | "An async Sender failed to handle a stop condition."; |
| 144 | |
| 145 | const char* name() const noexcept override; |
| 146 | const char* description() const noexcept override; |
| 147 | const char* what() const noexcept override; |
| 148 | int get_errno() const noexcept override; |
| 149 | }; |
| 150 | |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 151 | } // namespace exception |
| 152 | |
| 153 | using exception_t = exception::exception; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 154 | using internal_exception_t = exception::internal_exception; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 155 | |
| 156 | } // namespace sdbusplus |