Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <exception> |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame^] | 4 | #include <string> |
| 5 | #include <system_error> |
| 6 | #include <systemd/sd-bus.h> |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 7 | |
| 8 | namespace sdbusplus |
| 9 | { |
| 10 | |
| 11 | namespace exception |
| 12 | { |
| 13 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 14 | /** Base exception class for all sdbusplus exceptions, including those created |
| 15 | * by the bindings. */ |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 16 | struct exception : public std::exception |
| 17 | { |
Patrick Williams | ea24144 | 2016-11-15 14:41:13 -0600 | [diff] [blame] | 18 | virtual const char* name() const noexcept = 0; |
| 19 | virtual const char* description() const noexcept = 0; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 20 | }; |
| 21 | |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 22 | /** base exception class for all errors generated by sdbusplus itself. */ |
| 23 | struct internal_exception : public exception |
| 24 | { |
| 25 | }; |
| 26 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame^] | 27 | /** Exception for when an underlying sd_bus method call fails. */ |
| 28 | class SdBusError final : public internal_exception, public std::system_error |
| 29 | { |
| 30 | public: |
| 31 | /** Errno must be positive */ |
| 32 | SdBusError(int error, const char* prefix); |
| 33 | /** Becomes the owner of the error */ |
| 34 | SdBusError(sd_bus_error error, const char* prefix); |
| 35 | |
| 36 | SdBusError(const SdBusError&) = delete; |
| 37 | SdBusError& operator=(const SdBusError&) = delete; |
| 38 | SdBusError(SdBusError&& other); |
| 39 | SdBusError& operator=(SdBusError&& other); |
| 40 | virtual ~SdBusError(); |
| 41 | |
| 42 | const char* name() const noexcept override; |
| 43 | const char* description() const noexcept override; |
| 44 | const char* what() const noexcept override; |
| 45 | |
| 46 | private: |
| 47 | sd_bus_error error; |
| 48 | std::string full_message; |
| 49 | |
| 50 | /** Populates the full_message from the stored |
| 51 | * error and the passed in prefix. */ |
| 52 | void populateMessage(const char* prefix); |
| 53 | |
| 54 | /** Helper to reduce duplicate move logic */ |
| 55 | void move(SdBusError&& other); |
| 56 | }; |
| 57 | |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 58 | /** Exception for when an invalid conversion from string to enum is |
| 59 | * attempted. */ |
| 60 | struct InvalidEnumString final : public internal_exception |
| 61 | { |
| 62 | static constexpr auto errName = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 63 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString"; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 64 | static constexpr auto errDesc = |
| 65 | "An enumeration mapping was attempted for which no valid enumeration " |
| 66 | "value exists."; |
| 67 | static constexpr auto errWhat = |
Patrick Williams | 02d9675 | 2017-04-19 07:52:29 -0500 | [diff] [blame] | 68 | "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: " |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 69 | "An enumeration mapping was attempted for which no valid enumeration " |
| 70 | "value exists."; |
| 71 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 72 | const char* name() const noexcept override; |
| 73 | const char* description() const noexcept override; |
| 74 | const char* what() const noexcept override; |
Patrick Williams | 59b70d1 | 2016-11-16 16:11:25 -0600 | [diff] [blame] | 75 | }; |
| 76 | |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 77 | } // namespace exception |
| 78 | |
| 79 | using exception_t = exception::exception; |
Patrick Williams | 1a28306 | 2016-11-13 19:05:10 -0600 | [diff] [blame] | 80 | using internal_exception_t = exception::internal_exception; |
Patrick Williams | f4a6f41 | 2016-11-11 13:47:29 -0600 | [diff] [blame] | 81 | |
| 82 | } // namespace sdbusplus |