• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <brillo/errors/error_codes.h>
12 #include <brillo/strings/string_utils.h>
13 
14 namespace brillo {
15 namespace dbus_utils {
16 
CreateDBusErrorResponse(dbus::MethodCall * method_call,const std::string & error_name,const std::string & error_message)17 std::unique_ptr<dbus::Response> CreateDBusErrorResponse(
18     dbus::MethodCall* method_call,
19     const std::string& error_name,
20     const std::string& error_message) {
21   return dbus::ErrorResponse::FromMethodCall(method_call, error_name,
22                                              error_message);
23 }
24 
GetDBusError(dbus::MethodCall * method_call,const brillo::Error * error)25 std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call,
26                                              const brillo::Error* error) {
27   CHECK(error) << "Error object must be specified";
28   std::string error_name = DBUS_ERROR_FAILED;  // Default error code.
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) {
35     error_name = error->GetCode();
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 += ';';
45     error_message +=
46         error->GetDomain() + '/' + error->GetCode() + ':' + error->GetMessage();
47     error = error->GetInnerError();
48   }
49   return CreateDBusErrorResponse(method_call, error_name, error_message);
50 }
51 
AddDBusError(brillo::ErrorPtr * error,const std::string & dbus_error_name,const std::string & dbus_error_message)52 void AddDBusError(brillo::ErrorPtr* error,
53                   const std::string& dbus_error_name,
54                   const std::string& dbus_error_message) {
55   std::vector<std::string> parts = string_utils::Split(dbus_error_message, ";");
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(':');
61     if (slash_pos != std::string::npos && colon_pos != std::string::npos &&
62         slash_pos < colon_pos) {
63       // If we have both '/' and ':' and in proper order, then we have a
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 &&
70                colon_pos == std::string::npos && errors.empty()) {
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.
73       errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part);
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.
79       errors.emplace_back(
80           errors::dbus::kDomain, dbus_error_name, dbus_error_message);
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) {
87     Error::AddTo(
88         error, FROM_HERE, std::get<0>(*it), std::get<1>(*it), std::get<2>(*it));
89   }
90 }
91 
92 }  // namespace dbus_utils
93 }  // namespace brillo
94