blob: b1b70877cf54641ce6e3b8f0ed1efacb6c14d6ce [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
5#include <chromeos/dbus/utils.h>
6
Alex Vakulenko00ceab82014-11-04 22:21:597#include <tuple>
8#include <vector>
9
Alex Vakulenkob3813652014-08-27 15:04:2210#include <base/bind.h>
11#include <base/memory/scoped_ptr.h>
Alex Vakulenko15104662014-08-27 18:00:5712#include <chromeos/errors/error_codes.h>
Alex Vakulenko00ceab82014-11-04 22:21:5913#include <chromeos/strings/string_utils.h>
Alex Vakulenkob3813652014-08-27 15:04:2214
15namespace chromeos {
16namespace dbus_utils {
17
Alex Vakulenko1363ed62014-09-22 21:38:4618// Do not inline ScopedDBusErrorWrapper constructor/destructor.
19ScopedDBusErrorWrapper::ScopedDBusErrorWrapper() {}
20ScopedDBusErrorWrapper::~ScopedDBusErrorWrapper() {}
21
22// Delegate the call to dbus::ScopedDBusError::is_set().
23bool ScopedDBusErrorWrapper::is_set() const {
24 return dbus::ScopedDBusError::is_set();
25}
26
Alex Vakulenkob3813652014-08-27 15:04:2227std::unique_ptr<dbus::Response> CreateDBusErrorResponse(
28 dbus::MethodCall* method_call,
Alex Vakulenkof437e3b2014-10-30 23:28:3829 const std::string& error_name,
30 const std::string& error_message) {
31 auto resp = dbus::ErrorResponse::FromMethodCall(method_call, error_name,
32 error_message);
Alex Vakulenkob3813652014-08-27 15:04:2233 return std::unique_ptr<dbus::Response>(resp.release());
34}
35
36std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call,
37 const chromeos::Error* error) {
Alex Vakulenkof437e3b2014-10-30 23:28:3838 CHECK(error) << "Error object must be specified";
39 std::string error_name = DBUS_ERROR_FAILED; // Default error code.
Alex Vakulenkob3813652014-08-27 15:04:2240 std::string error_message;
41
42 // Special case for "dbus" error domain.
43 // Pop the error code and message from the error chain and use them as the
44 // actual D-Bus error message.
45 if (error->GetDomain() == errors::dbus::kDomain) {
Alex Vakulenkof437e3b2014-10-30 23:28:3846 error_name = error->GetCode();
Alex Vakulenkob3813652014-08-27 15:04:2247 error_message = error->GetMessage();
48 error = error->GetInnerError();
49 }
50
51 // Append any inner errors to the error message.
52 while (error) {
53 // Format error string as "domain/code:message".
54 if (!error_message.empty())
55 error_message += ';';
56 error_message += error->GetDomain() + '/' + error->GetCode() + ':' +
57 error->GetMessage();
58 error = error->GetInnerError();
59 }
Alex Vakulenkof437e3b2014-10-30 23:28:3860 return CreateDBusErrorResponse(method_call, error_name, error_message);
Alex Vakulenkob3813652014-08-27 15:04:2261}
62
Alex Vakulenko00ceab82014-11-04 22:21:5963void AddDBusError(chromeos::ErrorPtr* error,
Alex Vakulenkof437e3b2014-10-30 23:28:3864 const std::string& dbus_error_name,
Alex Vakulenko00ceab82014-11-04 22:21:5965 const std::string& dbus_error_message) {
66 std::vector<std::string> parts = string_utils::Split(dbus_error_message, ';');
67 std::vector<std::tuple<std::string, std::string, std::string>> errors;
68 for (const std::string& part : parts) {
69 // Each part should be in format of "domain/code:message"
70 size_t slash_pos = part.find('/');
71 size_t colon_pos = part.find(':');
72 if (slash_pos != std::string::npos &&
73 colon_pos != std::string::npos &&
74 slash_pos < colon_pos) {
Alex Vakulenkof437e3b2014-10-30 23:28:3875 // If we have both '/' and ':' and in proper order, then we have a
Alex Vakulenko00ceab82014-11-04 22:21:5976 // correctly encoded error object.
77 std::string domain = part.substr(0, slash_pos);
78 std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1);
79 std::string message = part.substr(colon_pos + 1);
80 errors.emplace_back(domain, code, message);
81 } else if (slash_pos == std::string::npos &&
82 colon_pos == std::string::npos &&
83 errors.empty()) {
84 // If we don't have both '/' and ':' and this is the first error object,
85 // then we had a D-Bus error at the top of the error chain.
Alex Vakulenkof437e3b2014-10-30 23:28:3886 errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part);
Alex Vakulenko00ceab82014-11-04 22:21:5987 } else {
88 // We have a malformed part. The whole D-Bus error was most likely
89 // not generated by GetDBusError(). To be safe, stop parsing it
90 // and return the error as received from D-Bus.
91 errors.clear(); // Remove any errors accumulated so far.
92 errors.emplace_back(errors::dbus::kDomain,
Alex Vakulenkof437e3b2014-10-30 23:28:3893 dbus_error_name,
Alex Vakulenko00ceab82014-11-04 22:21:5994 dbus_error_message);
95 break;
96 }
97 }
98
99 // Go backwards and add the parsed errors to the error chain.
100 for (auto it = errors.crbegin(); it != errors.crend(); ++it) {
101 Error::AddTo(error, std::get<0>(*it), std::get<1>(*it), std::get<2>(*it));
102 }
103}
104
Alex Vakulenkob3813652014-08-27 15:04:22105} // namespace dbus_utils
106} // namespace chromeos