Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 1 | #include <sdbusplus/exception.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 2 | |
Patrick Williams | b6f729d | 2021-09-02 15:02:19 -0500 | [diff] [blame] | 3 | #include <cerrno> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 4 | #include <stdexcept> |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 5 | #include <utility> |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 6 | |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 7 | #ifdef __clang__ |
| 8 | #pragma clang diagnostic push |
| 9 | #pragma clang diagnostic ignored "-Wc99-extensions" |
| 10 | #endif |
| 11 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 12 | namespace sdbusplus |
| 13 | { |
| 14 | namespace exception |
| 15 | { |
| 16 | |
Patrick Williams | a8e3502 | 2021-09-28 06:16:52 -0500 | [diff] [blame] | 17 | void exception::unused() const noexcept |
| 18 | {} |
| 19 | |
Patrick Williams | 1522866 | 2021-09-03 06:04:25 -0500 | [diff] [blame] | 20 | int generated_exception::get_errno() const noexcept |
| 21 | { |
| 22 | return EIO; |
| 23 | } |
| 24 | |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 25 | SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 26 | error(SD_BUS_ERROR_NULL), intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 27 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 28 | // We can't check the output of intf->sd_bus_error_set_errno() because |
| 29 | // it returns the input errorcode. We don't want to try and guess |
| 30 | // possible error statuses. Instead, check to see if the error was |
| 31 | // constructed to determine success. |
| 32 | intf->sd_bus_error_set_errno(&this->error, error); |
| 33 | if (!intf->sd_bus_error_is_set(&this->error)) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 34 | { |
William A. Kennington III | 2726c17 | 2018-06-22 19:39:04 -0700 | [diff] [blame] | 35 | throw std::runtime_error("Failed to create SdBusError"); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | populateMessage(prefix); |
| 39 | } |
| 40 | |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 41 | SdBusError::SdBusError(sd_bus_error* error, const char* prefix, |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 42 | SdBusInterface* intf) : |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 43 | error(*error), |
| 44 | intf(intf) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 45 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 46 | // We own the error so remove the caller's reference |
| 47 | *error = SD_BUS_ERROR_NULL; |
| 48 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 49 | populateMessage(prefix); |
| 50 | } |
| 51 | |
Vernon Mauery | fac43a6 | 2018-08-01 12:29:23 -0700 | [diff] [blame] | 52 | SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL) |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 53 | { |
| 54 | move(std::move(other)); |
| 55 | } |
| 56 | |
| 57 | SdBusError& SdBusError::operator=(SdBusError&& other) |
| 58 | { |
| 59 | if (this != &other) |
| 60 | { |
| 61 | move(std::move(other)); |
| 62 | } |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | SdBusError::~SdBusError() |
| 67 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 68 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const char* SdBusError::name() const noexcept |
| 72 | { |
| 73 | return error.name; |
| 74 | } |
| 75 | |
| 76 | const char* SdBusError::description() const noexcept |
| 77 | { |
| 78 | return error.message; |
| 79 | } |
| 80 | |
| 81 | const char* SdBusError::what() const noexcept |
| 82 | { |
| 83 | return full_message.c_str(); |
| 84 | } |
| 85 | |
Adriana Kobylak | 33335b3 | 2018-09-21 11:50:42 -0500 | [diff] [blame] | 86 | int SdBusError::get_errno() const noexcept |
| 87 | { |
| 88 | return intf->sd_bus_error_get_errno(&this->error); |
| 89 | } |
| 90 | |
Adrian Ambrożewicz | a66f6b4 | 2020-01-09 17:21:58 +0100 | [diff] [blame] | 91 | const sd_bus_error* SdBusError::get_error() const noexcept |
| 92 | { |
| 93 | return &error; |
| 94 | } |
| 95 | |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 96 | void SdBusError::populateMessage(const char* prefix) |
| 97 | { |
| 98 | full_message = prefix; |
| 99 | if (error.name) |
| 100 | { |
| 101 | full_message += ": "; |
| 102 | full_message += error.name; |
| 103 | } |
| 104 | if (error.message) |
| 105 | { |
| 106 | full_message += ": "; |
| 107 | full_message += error.message; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void SdBusError::move(SdBusError&& other) |
| 112 | { |
William A. Kennington III | 3765750 | 2018-06-22 19:00:05 -0700 | [diff] [blame] | 113 | intf = std::move(other.intf); |
| 114 | |
| 115 | intf->sd_bus_error_free(&error); |
William A. Kennington III | 20db3bf | 2018-05-14 16:18:37 -0700 | [diff] [blame] | 116 | error = other.error; |
| 117 | other.error = SD_BUS_ERROR_NULL; |
| 118 | |
| 119 | full_message = std::move(other.full_message); |
| 120 | } |
| 121 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 122 | const char* InvalidEnumString::name() const noexcept |
| 123 | { |
| 124 | return errName; |
| 125 | } |
| 126 | |
| 127 | const char* InvalidEnumString::description() const noexcept |
| 128 | { |
| 129 | return errDesc; |
| 130 | } |
| 131 | |
| 132 | const char* InvalidEnumString::what() const noexcept |
| 133 | { |
| 134 | return errWhat; |
| 135 | } |
| 136 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 137 | int InvalidEnumString::get_errno() const noexcept |
| 138 | { |
| 139 | return EINVAL; |
| 140 | } |
| 141 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 142 | UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn, |
| 143 | std::string_view reasonIn) : |
| 144 | propertyName(propertyNameIn), |
| 145 | reason(reasonIn), |
| 146 | errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName + |
| 147 | "', Reason: '" + reason + "'.") |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 148 | {} |
| 149 | |
| 150 | const char* UnpackPropertyError::name() const noexcept |
| 151 | { |
| 152 | return errName; |
| 153 | } |
| 154 | |
| 155 | const char* UnpackPropertyError::description() const noexcept |
| 156 | { |
| 157 | return errDesc; |
| 158 | } |
| 159 | |
| 160 | const char* UnpackPropertyError::what() const noexcept |
| 161 | { |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 162 | return errWhatDetailed.c_str(); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Patrick Williams | bd372ec | 2021-09-02 15:15:24 -0500 | [diff] [blame] | 165 | int UnpackPropertyError::get_errno() const noexcept |
| 166 | { |
| 167 | return EINVAL; |
| 168 | } |
| 169 | |
Patrick Williams | 93b0e70 | 2017-04-18 11:19:45 -0500 | [diff] [blame] | 170 | } // namespace exception |
| 171 | } // namespace sdbusplus |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 172 | |
| 173 | #ifdef __clang__ |
| 174 | #pragma clang diagnostic pop |
| 175 | #endif |