blob: db3b151f6d13a8818b10f055529ed327c7017fd1 [file] [log] [blame]
Alex Vakulenkob3813652014-08-27 08:04:22 -07001// 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 <chromeos/dbus/utils.h>
6
Alex Vakulenko00ceab82014-11-04 14:21:59 -08007#include <tuple>
8#include <vector>
9
Alex Vakulenkob3813652014-08-27 08:04:22 -070010#include <base/bind.h>
11#include <base/memory/scoped_ptr.h>
Alex Vakulenko15104662014-08-27 11:00:57 -070012#include <chromeos/errors/error_codes.h>
Alex Vakulenko00ceab82014-11-04 14:21:59 -080013#include <chromeos/strings/string_utils.h>
Alex Vakulenkob3813652014-08-27 08:04:22 -070014
15namespace chromeos {
16namespace dbus_utils {
17
Alex Vakulenkob3813652014-08-27 08:04:22 -070018std::unique_ptr<dbus::Response> CreateDBusErrorResponse(
19 dbus::MethodCall* method_call,
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070020 const std::string& error_name,
21 const std::string& error_message) {
22 auto resp = dbus::ErrorResponse::FromMethodCall(method_call, error_name,
23 error_message);
Alex Vakulenkob3813652014-08-27 08:04:22 -070024 return std::unique_ptr<dbus::Response>(resp.release());
25}
26
27std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call,
28 const chromeos::Error* error) {
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070029 CHECK(error) << "Error object must be specified";
30 std::string error_name = DBUS_ERROR_FAILED; // Default error code.
Alex Vakulenkob3813652014-08-27 08:04:22 -070031 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) {
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070037 error_name = error->GetCode();
Alex Vakulenkob3813652014-08-27 08:04:22 -070038 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 += error->GetDomain() + '/' + error->GetCode() + ':' +
48 error->GetMessage();
49 error = error->GetInnerError();
50 }
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070051 return CreateDBusErrorResponse(method_call, error_name, error_message);
Alex Vakulenkob3813652014-08-27 08:04:22 -070052}
53
Alex Vakulenko00ceab82014-11-04 14:21:59 -080054void AddDBusError(chromeos::ErrorPtr* error,
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070055 const std::string& dbus_error_name,
Alex Vakulenko00ceab82014-11-04 14:21:59 -080056 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 &&
64 colon_pos != std::string::npos &&
65 slash_pos < colon_pos) {
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070066 // If we have both '/' and ':' and in proper order, then we have a
Alex Vakulenko00ceab82014-11-04 14:21:59 -080067 // correctly encoded error object.
68 std::string domain = part.substr(0, slash_pos);
69 std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1);
70 std::string message = part.substr(colon_pos + 1);
71 errors.emplace_back(domain, code, message);
72 } else if (slash_pos == std::string::npos &&
73 colon_pos == std::string::npos &&
74 errors.empty()) {
75 // If we don't have both '/' and ':' and this is the first error object,
76 // then we had a D-Bus error at the top of the error chain.
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070077 errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part);
Alex Vakulenko00ceab82014-11-04 14:21:59 -080078 } else {
79 // We have a malformed part. The whole D-Bus error was most likely
80 // not generated by GetDBusError(). To be safe, stop parsing it
81 // and return the error as received from D-Bus.
82 errors.clear(); // Remove any errors accumulated so far.
83 errors.emplace_back(errors::dbus::kDomain,
Alex Vakulenkof437e3b2014-10-30 16:28:38 -070084 dbus_error_name,
Alex Vakulenko00ceab82014-11-04 14:21:59 -080085 dbus_error_message);
86 break;
87 }
88 }
89
90 // Go backwards and add the parsed errors to the error chain.
91 for (auto it = errors.crbegin(); it != errors.crend(); ++it) {
Alex Vakulenko8f815f52014-11-11 11:42:05 -080092 Error::AddTo(error, FROM_HERE,
93 std::get<0>(*it), std::get<1>(*it), std::get<2>(*it));
Alex Vakulenko00ceab82014-11-04 14:21:59 -080094 }
95}
96
Alex Vakulenkob3813652014-08-27 08:04:22 -070097} // namespace dbus_utils
98} // namespace chromeos