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