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); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 55 | /** Becomes the owner of the error */ |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 56 | SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 57 | SdBusInterface* intf = &sdbus_impl); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 58 | |
| 59 | SdBusError(const SdBusError&) = delete; |
| 60 | SdBusError& operator=(const SdBusError&) = delete; |
| 61 | SdBusError(SdBusError&& other); |
| 62 | SdBusError& operator=(SdBusError&& other); |
Brad Bishop | 0eda6eb | 2022-09-29 11:04:13 -0400 | [diff] [blame] | 63 | ~SdBusError() override; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 64 | |
| 65 | const char* name() const noexcept override; |
| 66 | const char* description() const noexcept override; |
| 67 | const char* what() const noexcept override; |
Patrick Williams | b6f729d | 2021-09-02 15:02:19 -0500 | [diff] [blame] | 68 | int get_errno() const noexcept override; |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 69 | const sd_bus_error* get_error() const noexcept; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | sd_bus_error error; |
| 73 | std::string full_message; |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 74 | SdBusInterface* intf; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 75 | |
| 76 | /** Populates the full_message from the stored |
| 77 | * error and the passed in prefix. */ |
| 78 | void populateMessage(const char* prefix); |
| 79 | |
| 80 | /** Helper to reduce duplicate move logic */ |
| 81 | void move(SdBusError&& other); |
| 82 | }; |
| 83 | |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 84 | /** Exception for when an invalid conversion from string to enum is |
| 85 | * attempted. */ |
| 86 | struct InvalidEnumString final : public internal_exception |
| 87 | { |
| 88 | static constexpr auto errName = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 89 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString"; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 90 | static constexpr auto errDesc = |
| 91 | "An enumeration mapping was attempted for which no valid enumeration " |
| 92 | "value exists."; |
| 93 | static constexpr auto errWhat = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 94 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: " |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 95 | "An enumeration mapping was attempted for which no valid enumeration " |
| 96 | "value exists."; |
| 97 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 98 | const char* name() const noexcept override; |
| 99 | const char* description() const noexcept override; |
| 100 | const char* what() const noexcept override; |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 101 | int get_errno() const noexcept override; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 102 | }; |
| 103 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 104 | /** Exception for when unpackProperties cannot find given property in provided |
| 105 | * container */ |
| 106 | class UnpackPropertyError final : public internal_exception |
| 107 | { |
| 108 | public: |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 109 | UnpackPropertyError(std::string_view propertyName, |
| 110 | const UnpackErrorReason reason); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 111 | |
| 112 | static constexpr auto errName = |
| 113 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError"; |
| 114 | static constexpr auto errDesc = |
| 115 | "unpackProperties failed to unpack one of requested properties."; |
| 116 | static constexpr auto errWhat = |
| 117 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: " |
| 118 | "unpackProperties failed to unpack one of requested properties."; |
| 119 | |
| 120 | const char* name() const noexcept override; |
| 121 | const char* description() const noexcept override; |
| 122 | const char* what() const noexcept override; |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 123 | int get_errno() const noexcept override; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 124 | |
| 125 | const std::string propertyName; |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 126 | const UnpackErrorReason reason; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | const std::string errWhatDetailed; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 132 | } // namespace exception |
| 133 | |
| 134 | using exception_t = exception::exception; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 135 | using internal_exception_t = exception::internal_exception; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 136 | |
| 137 | } // namespace sdbusplus |