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 | |
| 13 | namespace exception |
| 14 | { |
| 15 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 16 | /** Base exception class for all sdbusplus exceptions, including those created |
| 17 | * by the bindings. */ |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 18 | struct exception : public std::exception |
| 19 | { |
Patrick Williams | ea24144 | 2016-11-15 14:41:13 -0600 | [diff] [blame] | 20 | virtual const char* name() const noexcept = 0; |
| 21 | virtual const char* description() const noexcept = 0; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 22 | }; |
| 23 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 24 | /** base exception class for all errors generated by sdbusplus itself. */ |
| 25 | struct internal_exception : public exception |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 26 | {}; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 27 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 28 | /** Exception for when an underlying sd_bus method call fails. */ |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 29 | class SdBusError final : public internal_exception |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | /** Errno must be positive */ |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 33 | SdBusError(int error, const char* prefix, |
| 34 | SdBusInterface* intf = &sdbus_impl); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 35 | /** Becomes the owner of the error */ |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 36 | SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 37 | SdBusInterface* intf = &sdbus_impl); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 38 | |
| 39 | SdBusError(const SdBusError&) = delete; |
| 40 | SdBusError& operator=(const SdBusError&) = delete; |
| 41 | SdBusError(SdBusError&& other); |
| 42 | SdBusError& operator=(SdBusError&& other); |
| 43 | virtual ~SdBusError(); |
| 44 | |
| 45 | const char* name() const noexcept override; |
| 46 | const char* description() const noexcept override; |
| 47 | const char* what() const noexcept override; |
Adriana Kobylak | 33335b3 | 2018-09-21 11:50:42 -0500 | [diff] [blame] | 48 | int get_errno() const noexcept; |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 49 | const sd_bus_error* get_error() const noexcept; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | sd_bus_error error; |
| 53 | std::string full_message; |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 54 | SdBusInterface* intf; |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 55 | |
| 56 | /** Populates the full_message from the stored |
| 57 | * error and the passed in prefix. */ |
| 58 | void populateMessage(const char* prefix); |
| 59 | |
| 60 | /** Helper to reduce duplicate move logic */ |
| 61 | void move(SdBusError&& other); |
| 62 | }; |
| 63 | |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 64 | /** Exception for when an invalid conversion from string to enum is |
| 65 | * attempted. */ |
| 66 | struct InvalidEnumString final : public internal_exception |
| 67 | { |
| 68 | static constexpr auto errName = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 69 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString"; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 70 | static constexpr auto errDesc = |
| 71 | "An enumeration mapping was attempted for which no valid enumeration " |
| 72 | "value exists."; |
| 73 | static constexpr auto errWhat = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 74 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: " |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 75 | "An enumeration mapping was attempted for which no valid enumeration " |
| 76 | "value exists."; |
| 77 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 78 | const char* name() const noexcept override; |
| 79 | const char* description() const noexcept override; |
| 80 | const char* what() const noexcept override; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 81 | }; |
| 82 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 83 | /** Exception for when unpackProperties cannot find given property in provided |
| 84 | * container */ |
| 85 | class UnpackPropertyError final : public internal_exception |
| 86 | { |
| 87 | public: |
| 88 | UnpackPropertyError(std::string_view propertyName, std::string_view reason); |
| 89 | |
| 90 | static constexpr std::string_view reasonMissingProperty = |
| 91 | "Missing property"; |
| 92 | static constexpr std::string_view reasonTypeNotMatched = "Type not matched"; |
| 93 | |
| 94 | static constexpr auto errName = |
| 95 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError"; |
| 96 | static constexpr auto errDesc = |
| 97 | "unpackProperties failed to unpack one of requested properties."; |
| 98 | static constexpr auto errWhat = |
| 99 | "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: " |
| 100 | "unpackProperties failed to unpack one of requested properties."; |
| 101 | |
| 102 | const char* name() const noexcept override; |
| 103 | const char* description() const noexcept override; |
| 104 | const char* what() const noexcept override; |
| 105 | |
| 106 | const std::string propertyName; |
| 107 | const std::string reason; |
| 108 | }; |
| 109 | |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 110 | } // namespace exception |
| 111 | |
| 112 | using exception_t = exception::exception; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 113 | using internal_exception_t = exception::internal_exception; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 114 | |
| 115 | } // namespace sdbusplus |