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