blob: c9bb4db0251d64931839244cb2a2518b52f9d331 [file] [log] [blame]
Alex Vakulenkob3813652014-08-27 15:04:221// 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 Vakulenko9ed0cab2015-10-12 22:21:285#include <brillo/dbus/utils.h>
Alex Vakulenkob3813652014-08-27 15:04:226
Alex Vakulenko00ceab82014-11-04 22:21:597#include <tuple>
8#include <vector>
9
Alex Vakulenkob3813652014-08-27 15:04:2210#include <base/bind.h>
Alex Vakulenko9ed0cab2015-10-12 22:21:2811#include <brillo/errors/error_codes.h>
12#include <brillo/strings/string_utils.h>
Alex Vakulenkob3813652014-08-27 15:04:2213
Alex Vakulenko9ed0cab2015-10-12 22:21:2814namespace brillo {
Alex Vakulenkob3813652014-08-27 15:04:2215namespace dbus_utils {
16
Alex Vakulenkob3813652014-08-27 15:04:2217std::unique_ptr<dbus::Response> CreateDBusErrorResponse(
18 dbus::MethodCall* method_call,
Alex Vakulenkof437e3b2014-10-30 23:28:3819 const std::string& error_name,
20 const std::string& error_message) {
Alex Vakulenko8c0f1922016-04-08 15:56:2421 return dbus::ErrorResponse::FromMethodCall(method_call, error_name,
22 error_message);
Alex Vakulenkob3813652014-08-27 15:04:2223}
24
25std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call,
Alex Vakulenko9ed0cab2015-10-12 22:21:2826 const brillo::Error* error) {
Alex Vakulenkof437e3b2014-10-30 23:28:3827 CHECK(error) << "Error object must be specified";
28 std::string error_name = DBUS_ERROR_FAILED; // Default error code.
Alex Vakulenkob3813652014-08-27 15:04:2229 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 Vakulenkof437e3b2014-10-30 23:28:3835 error_name = error->GetCode();
Alex Vakulenkob3813652014-08-27 15:04:2236 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 Vakulenko05d29042015-01-13 17:39:2545 error_message +=
46 error->GetDomain() + '/' + error->GetCode() + ':' + error->GetMessage();
Alex Vakulenkob3813652014-08-27 15:04:2247 error = error->GetInnerError();
48 }
Alex Vakulenkof437e3b2014-10-30 23:28:3849 return CreateDBusErrorResponse(method_call, error_name, error_message);
Alex Vakulenkob3813652014-08-27 15:04:2250}
51
Alex Vakulenko9ed0cab2015-10-12 22:21:2852void AddDBusError(brillo::ErrorPtr* error,
Alex Vakulenkof437e3b2014-10-30 23:28:3853 const std::string& dbus_error_name,
Alex Vakulenko00ceab82014-11-04 22:21:5954 const std::string& dbus_error_message) {
Vitaly Buka852ff002015-03-11 02:33:3355 std::vector<std::string> parts = string_utils::Split(dbus_error_message, ";");
Alex Vakulenko00ceab82014-11-04 22:21:5956 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 Vakulenko05d29042015-01-13 17:39:2561 if (slash_pos != std::string::npos && colon_pos != std::string::npos &&
Alex Vakulenko00ceab82014-11-04 22:21:5962 slash_pos < colon_pos) {
Alex Vakulenkof437e3b2014-10-30 23:28:3863 // If we have both '/' and ':' and in proper order, then we have a
Alex Vakulenko00ceab82014-11-04 22:21:5964 // 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 Vakulenko05d29042015-01-13 17:39:2570 colon_pos == std::string::npos && errors.empty()) {
Alex Vakulenko00ceab82014-11-04 22:21:5971 // 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 Vakulenkof437e3b2014-10-30 23:28:3873 errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part);
Alex Vakulenko00ceab82014-11-04 22:21:5974 } 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 Vakulenko05d29042015-01-13 17:39:2579 errors.emplace_back(
80 errors::dbus::kDomain, dbus_error_name, dbus_error_message);
Alex Vakulenko00ceab82014-11-04 22:21:5981 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 Vakulenko05d29042015-01-13 17:39:2587 Error::AddTo(
88 error, FROM_HERE, std::get<0>(*it), std::get<1>(*it), std::get<2>(*it));
Alex Vakulenko00ceab82014-11-04 22:21:5989 }
90}
91
Alex Vakulenkob3813652014-08-27 15:04:2292} // namespace dbus_utils
Alex Vakulenko9ed0cab2015-10-12 22:21:2893} // namespace brillo