aboutsummaryrefslogtreecommitdiffstats
path: root/chromeos/errors/error.cc
blob: b3fa711ddebc0f7b9d45ea5b2ed3df5348f76f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <chromeos/errors/error.h>

#include <base/logging.h>
#include <base/strings/stringprintf.h>

using chromeos::Error;
using chromeos::ErrorPtr;

namespace {
inline void LogError(const tracked_objects::Location& location,
                     const std::string& domain,
                     const std::string& code,
                     const std::string& message) {
  // Use logging::LogMessage() directly instead of LOG(ERROR) to substitute
  // the current error location with the location passed in to the Error object.
  // This way the log will contain the actual location of the error, and not
  // as if it always comes from chromeos/errors/error.cc(22).
  logging::LogMessage(
      location.file_name(), location.line_number(), logging::LOG_ERROR).stream()
      << location.function_name() << "(...): "
      << "Domain=" << domain << ", Code=" << code << ", Message=" << message;
}
}  // anonymous namespace

ErrorPtr Error::Create(const tracked_objects::Location& location,
                       const std::string& domain,
                       const std::string& code,
                       const std::string& message) {
  return Create(location, domain, code, message, ErrorPtr());
}

ErrorPtr Error::Create(const tracked_objects::Location& location,
                       const std::string& domain,
                       const std::string& code,
                       const std::string& message,
                       ErrorPtr inner_error) {
  LogError(location, domain, code, message);
  return ErrorPtr(
      new Error(location, domain, code, message, std::move(inner_error)));
}

void Error::AddTo(ErrorPtr* error,
                  const tracked_objects::Location& location,
                  const std::string& domain,
                  const std::string& code,
                  const std::string& message) {
  if (error) {
    *error = Create(location, domain, code, message, std::move(*error));
  } else {
    // Create already logs the error, but if |error| is nullptr,
    // we still want to log the error...
    LogError(location, domain, code, message);
  }
}

void Error::AddToPrintf(ErrorPtr* error,
                        const tracked_objects::Location& location,
                        const std::string& domain,
                        const std::string& code,
                        const char* format,
                        ...) {
  va_list ap;
  va_start(ap, format);
  std::string message = base::StringPrintV(format, ap);
  va_end(ap);
  AddTo(error, location, domain, code, message);
}

ErrorPtr Error::Clone() const {
  ErrorPtr inner_error = inner_error_ ? inner_error_->Clone() : nullptr;
  return ErrorPtr(
      new Error(location_, domain_, code_, message_, std::move(inner_error)));
}

bool Error::HasDomain(const std::string& domain) const {
  return FindErrorOfDomain(this, domain) != nullptr;
}

bool Error::HasError(const std::string& domain, const std::string& code) const {
  return FindError(this, domain, code) != nullptr;
}

const Error* Error::GetFirstError() const {
  const Error* err = this;
  while (err->GetInnerError())
    err = err->GetInnerError();
  return err;
}

Error::Error(const tracked_objects::Location& location,
             const std::string& domain,
             const std::string& code,
             const std::string& message,
             ErrorPtr inner_error)
    : Error{tracked_objects::LocationSnapshot{location},
            domain,
            code,
            message,
            std::move(inner_error)} {
}

Error::Error(const tracked_objects::LocationSnapshot& location,
             const std::string& domain,
             const std::string& code,
             const std::string& message,
             ErrorPtr inner_error)
    : domain_(domain),
      code_(code),
      message_(message),
      location_(location),
      inner_error_(std::move(inner_error)) {
}

const Error* Error::FindErrorOfDomain(const Error* error_chain_start,
                                      const std::string& domain) {
  while (error_chain_start) {
    if (error_chain_start->GetDomain() == domain)
      break;
    error_chain_start = error_chain_start->GetInnerError();
  }
  return error_chain_start;
}

const Error* Error::FindError(const Error* error_chain_start,
                              const std::string& domain,
                              const std::string& code) {
  while (error_chain_start) {
    if (error_chain_start->GetDomain() == domain &&
        error_chain_start->GetCode() == code)
      break;
    error_chain_start = error_chain_start->GetInnerError();
  }
  return error_chain_start;
}