blob: 3f58dd8d03cb89b2247439103a731adccc1c155e [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 Vakulenkob3813652014-08-27 15:04:2218std::unique_ptr<dbus::Response> CreateDBusErrorResponse(
19 dbus::MethodCall* method_call,
Alex Vakulenkof437e3b2014-10-30 23:28:3820 const std::string& error_name,
21 const std::string& error_message) {
Alex Vakulenko05d29042015-01-13 17:39:2522 auto resp = dbus::ErrorResponse::FromMethodCall(
23 method_call, error_name, error_message);
Alex Vakulenkob3813652014-08-27 15:04:2224 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 23:28:3829 CHECK(error) << "Error object must be specified";
30 std::string error_name = DBUS_ERROR_FAILED; // Default error code.
Alex Vakulenkob3813652014-08-27 15:04:2231 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 23:28:3837 error_name = error->GetCode();
Alex Vakulenkob3813652014-08-27 15:04:2238 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 += ';';
Alex Vakulenko05d29042015-01-13 17:39:2547 error_message +=
48 error->GetDomain() + '/' + error->GetCode() + ':' + error->GetMessage();
Alex Vakulenkob3813652014-08-27 15:04:2249 error = error->GetInnerError();
50 }
Alex Vakulenkof437e3b2014-10-30 23:28:3851 return CreateDBusErrorResponse(method_call, error_name, error_message);
Alex Vakulenkob3813652014-08-27 15:04:2252}
53
Alex Vakulenko00ceab82014-11-04 22:21:5954void AddDBusError(chromeos::ErrorPtr* error,
Alex Vakulenkof437e3b2014-10-30 23:28:3855 const std::string& dbus_error_name,
Alex Vakulenko00ceab82014-11-04 22:21:5956 const std::string& dbus_error_message) {
Vitaly Buka852ff002015-03-11 02:33:3357 std::vector<std::string> parts = string_utils::Split(dbus_error_message, ";");
Alex Vakulenko00ceab82014-11-04 22:21:5958 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(':');
Alex Vakulenko05d29042015-01-13 17:39:2563 if (slash_pos != std::string::npos && colon_pos != std::string::npos &&
Alex Vakulenko00ceab82014-11-04 22:21:5964 slash_pos < colon_pos) {
Alex Vakulenkof437e3b2014-10-30 23:28:3865 // If we have both '/' and ':' and in proper order, then we have a
Alex Vakulenko00ceab82014-11-04 22:21:5966 // correctly encoded error object.
67 std::string domain = part.substr(0, slash_pos);
68 std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1);
69 std::string message = part.substr(colon_pos + 1);
70 errors.emplace_back(domain, code, message);
71 } else if (slash_pos == std::string::npos &&
Alex Vakulenko05d29042015-01-13 17:39:2572 colon_pos == std::string::npos && errors.empty()) {
Alex Vakulenko00ceab82014-11-04 22:21:5973 // If we don't have both '/' and ':' and this is the first error object,
74 // then we had a D-Bus error at the top of the error chain.
Alex Vakulenkof437e3b2014-10-30 23:28:3875 errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part);
Alex Vakulenko00ceab82014-11-04 22:21:5976 } else {
77 // We have a malformed part. The whole D-Bus error was most likely
78 // not generated by GetDBusError(). To be safe, stop parsing it
79 // and return the error as received from D-Bus.
80 errors.clear(); // Remove any errors accumulated so far.
Alex Vakulenko05d29042015-01-13 17:39:2581 errors.emplace_back(
82 errors::dbus::kDomain, dbus_error_name, dbus_error_message);
Alex Vakulenko00ceab82014-11-04 22:21:5983 break;
84 }
85 }
86
87 // Go backwards and add the parsed errors to the error chain.
88 for (auto it = errors.crbegin(); it != errors.crend(); ++it) {
Alex Vakulenko05d29042015-01-13 17:39:2589 Error::AddTo(
90 error, FROM_HERE, std::get<0>(*it), std::get<1>(*it), std::get<2>(*it));
Alex Vakulenko00ceab82014-11-04 22:21:5991 }
92}
93
Alex Vakulenkob3813652014-08-27 15:04:2294} // namespace dbus_utils
95} // namespace chromeos