| // Copyright 2014 The ChromiumOS Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <brillo/dbus/utils.h> | |
| #include <tuple> | |
| #include <utility> | |
| #include <vector> | |
| #include <base/strings/strcat.h> | |
| #include <dbus/dbus-protocol.h> | |
| #include <brillo/errors/error_codes.h> | |
| #include <brillo/strings/string_utils.h> | |
| namespace brillo { | |
| namespace dbus_utils { | |
| dbus::Error ToDBusError(const brillo::Error& error) { | |
| std::string error_name = DBUS_ERROR_FAILED; // Default error code. | |
| std::string error_message; | |
| const brillo::Error* current_error = &error; | |
| // Special case for "dbus" error domain. | |
| // Pop the error code and message from the error chain and use them as the | |
| // actual D-Bus error message. | |
| if (current_error->GetDomain() == errors::dbus::kDomain) { | |
| error_name = current_error->GetCode(); | |
| error_message = current_error->GetMessage(); | |
| current_error = current_error->GetInnerError(); | |
| } | |
| // Append any inner errors to the error message. | |
| while (current_error) { | |
| // Format error string as "domain/code:message". | |
| if (!error_message.empty()) | |
| error_message += ';'; | |
| base::StrAppend(&error_message, | |
| {current_error->GetDomain(), "/", current_error->GetCode(), | |
| ":", current_error->GetMessage()}); | |
| current_error = current_error->GetInnerError(); | |
| } | |
| return dbus::Error(std::move(error_name), std::move(error_message)); | |
| } | |
| void AddDBusError(brillo::ErrorPtr* error, | |
| const std::string& dbus_error_name, | |
| const std::string& dbus_error_message) { | |
| std::vector<std::string> parts = string_utils::Split(dbus_error_message, ";"); | |
| std::vector<std::tuple<std::string, std::string, std::string>> errors; | |
| for (const std::string& part : parts) { | |
| // Each part should be in format of "domain/code:message" | |
| size_t slash_pos = part.find('/'); | |
| size_t colon_pos = part.find(':'); | |
| if (slash_pos != std::string::npos && colon_pos != std::string::npos && | |
| slash_pos < colon_pos) { | |
| // If we have both '/' and ':' and in proper order, then we have a | |
| // correctly encoded error object. | |
| std::string domain = part.substr(0, slash_pos); | |
| std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1); | |
| std::string message = part.substr(colon_pos + 1); | |
| errors.emplace_back(domain, code, message); | |
| } else if (slash_pos == std::string::npos && | |
| colon_pos == std::string::npos && errors.empty()) { | |
| // If we don't have both '/' and ':' and this is the first error object, | |
| // then we had a D-Bus error at the top of the error chain. | |
| errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part); | |
| } else { | |
| // We have a malformed part. The whole D-Bus error was most likely | |
| // not generated by GetDBusError(). To be safe, stop parsing it | |
| // and return the error as received from D-Bus. | |
| errors.clear(); // Remove any errors accumulated so far. | |
| errors.emplace_back(errors::dbus::kDomain, dbus_error_name, | |
| dbus_error_message); | |
| break; | |
| } | |
| } | |
| // Go backwards and add the parsed errors to the error chain. | |
| for (auto it = errors.crbegin(); it != errors.crend(); ++it) { | |
| Error::AddTo(error, FROM_HERE, std::get<0>(*it), std::get<1>(*it), | |
| std::get<2>(*it)); | |
| } | |
| } | |
| } // namespace dbus_utils | |
| } // namespace brillo |