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