Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 5 | #include <brillo/dbus/utils.h> |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 6 | |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 7 | #include <tuple> |
| 8 | #include <vector> |
| 9 | |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 10 | #include <base/bind.h> |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 11 | #include <brillo/errors/error_codes.h> |
| 12 | #include <brillo/strings/string_utils.h> |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 13 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 14 | namespace brillo { |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 15 | namespace dbus_utils { |
| 16 | |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 17 | std::unique_ptr<dbus::Response> CreateDBusErrorResponse( |
| 18 | dbus::MethodCall* method_call, |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 19 | const std::string& error_name, |
| 20 | const std::string& error_message) { |
Alex Vakulenko | 8c0f192 | 2016-04-08 15:56:24 | [diff] [blame] | 21 | return dbus::ErrorResponse::FromMethodCall(method_call, error_name, |
| 22 | error_message); |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call, |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 26 | const brillo::Error* error) { |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 27 | CHECK(error) << "Error object must be specified"; |
| 28 | std::string error_name = DBUS_ERROR_FAILED; // Default error code. |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 29 | std::string error_message; |
| 30 | |
| 31 | // Special case for "dbus" error domain. |
| 32 | // Pop the error code and message from the error chain and use them as the |
| 33 | // actual D-Bus error message. |
| 34 | if (error->GetDomain() == errors::dbus::kDomain) { |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 35 | error_name = error->GetCode(); |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 36 | error_message = error->GetMessage(); |
| 37 | error = error->GetInnerError(); |
| 38 | } |
| 39 | |
| 40 | // Append any inner errors to the error message. |
| 41 | while (error) { |
| 42 | // Format error string as "domain/code:message". |
| 43 | if (!error_message.empty()) |
| 44 | error_message += ';'; |
Alex Vakulenko | 05d2904 | 2015-01-13 17:39:25 | [diff] [blame] | 45 | error_message += |
| 46 | error->GetDomain() + '/' + error->GetCode() + ':' + error->GetMessage(); |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 47 | error = error->GetInnerError(); |
| 48 | } |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 49 | return CreateDBusErrorResponse(method_call, error_name, error_message); |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 50 | } |
| 51 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 52 | void AddDBusError(brillo::ErrorPtr* error, |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 53 | const std::string& dbus_error_name, |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 54 | const std::string& dbus_error_message) { |
Vitaly Buka | 852ff00 | 2015-03-11 02:33:33 | [diff] [blame] | 55 | std::vector<std::string> parts = string_utils::Split(dbus_error_message, ";"); |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 56 | std::vector<std::tuple<std::string, std::string, std::string>> errors; |
| 57 | for (const std::string& part : parts) { |
| 58 | // Each part should be in format of "domain/code:message" |
| 59 | size_t slash_pos = part.find('/'); |
| 60 | size_t colon_pos = part.find(':'); |
Alex Vakulenko | 05d2904 | 2015-01-13 17:39:25 | [diff] [blame] | 61 | if (slash_pos != std::string::npos && colon_pos != std::string::npos && |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 62 | slash_pos < colon_pos) { |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 63 | // If we have both '/' and ':' and in proper order, then we have a |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 64 | // correctly encoded error object. |
| 65 | std::string domain = part.substr(0, slash_pos); |
| 66 | std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1); |
| 67 | std::string message = part.substr(colon_pos + 1); |
| 68 | errors.emplace_back(domain, code, message); |
| 69 | } else if (slash_pos == std::string::npos && |
Alex Vakulenko | 05d2904 | 2015-01-13 17:39:25 | [diff] [blame] | 70 | colon_pos == std::string::npos && errors.empty()) { |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 71 | // If we don't have both '/' and ':' and this is the first error object, |
| 72 | // then we had a D-Bus error at the top of the error chain. |
Alex Vakulenko | f437e3b | 2014-10-30 23:28:38 | [diff] [blame] | 73 | errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part); |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 74 | } else { |
| 75 | // We have a malformed part. The whole D-Bus error was most likely |
| 76 | // not generated by GetDBusError(). To be safe, stop parsing it |
| 77 | // and return the error as received from D-Bus. |
| 78 | errors.clear(); // Remove any errors accumulated so far. |
Alex Vakulenko | 05d2904 | 2015-01-13 17:39:25 | [diff] [blame] | 79 | errors.emplace_back( |
| 80 | errors::dbus::kDomain, dbus_error_name, dbus_error_message); |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Go backwards and add the parsed errors to the error chain. |
| 86 | for (auto it = errors.crbegin(); it != errors.crend(); ++it) { |
Alex Vakulenko | 05d2904 | 2015-01-13 17:39:25 | [diff] [blame] | 87 | Error::AddTo( |
| 88 | error, FROM_HERE, std::get<0>(*it), std::get<1>(*it), std::get<2>(*it)); |
Alex Vakulenko | 00ceab8 | 2014-11-04 22:21:59 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Alex Vakulenko | b381365 | 2014-08-27 15:04:22 | [diff] [blame] | 92 | } // namespace dbus_utils |
Alex Vakulenko | 9ed0cab | 2015-10-12 22:21:28 | [diff] [blame] | 93 | } // namespace brillo |