• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium 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 "sync/api/sync_error.h"
6 
7 #include <ostream>
8 
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "sync/internal_api/public/base/model_type.h"
12 
13 namespace syncer {
14 
SyncError()15 SyncError::SyncError() {
16   Clear();
17 }
18 
SyncError(const tracked_objects::Location & location,ErrorType error_type,const std::string & custom_message,ModelType model_type)19 SyncError::SyncError(const tracked_objects::Location& location,
20                      ErrorType error_type,
21                      const std::string& custom_message,
22                      ModelType model_type) {
23   std::string type_message;
24   switch (error_type) {
25     case UNRECOVERABLE_ERROR:
26       type_message = "unrecoverable error was encountered: ";
27       break;
28     case DATATYPE_ERROR:
29       type_message = "datatype error was encountered: ";
30       break;
31     case PERSISTENCE_ERROR:
32       type_message = "persistence error was encountered: ";
33       break;
34     case CRYPTO_ERROR:
35       type_message = "cryptographer error was encountered: ";
36       break;
37     case UNREADY_ERROR:
38       type_message = "unready error was encountered: ";
39       break;
40     case UNSET:
41       NOTREACHED() << "Invalid error type";
42       return;
43   }
44   Init(location, type_message + custom_message, model_type, error_type);
45   PrintLogError();
46 }
47 
SyncError(const SyncError & other)48 SyncError::SyncError(const SyncError& other) {
49   Copy(other);
50 }
51 
~SyncError()52 SyncError::~SyncError() {
53 }
54 
operator =(const SyncError & other)55 SyncError& SyncError::operator=(const SyncError& other) {
56   if (this == &other) {
57     return *this;
58   }
59   Copy(other);
60   return *this;
61 }
62 
Copy(const SyncError & other)63 void SyncError::Copy(const SyncError& other) {
64   if (other.IsSet()) {
65     Init(other.location(),
66          other.message(),
67          other.model_type(),
68          other.error_type());
69   } else {
70     Clear();
71   }
72 }
73 
Clear()74 void SyncError::Clear() {
75   location_.reset();
76   message_ = std::string();
77   model_type_ = UNSPECIFIED;
78   error_type_ = UNSET;
79 }
80 
Reset(const tracked_objects::Location & location,const std::string & message,ModelType model_type)81 void SyncError::Reset(const tracked_objects::Location& location,
82                       const std::string& message,
83                       ModelType model_type) {
84   Init(location, message, model_type, DATATYPE_ERROR);
85   PrintLogError();
86 }
87 
Init(const tracked_objects::Location & location,const std::string & message,ModelType model_type,ErrorType error_type)88 void SyncError::Init(const tracked_objects::Location& location,
89                      const std::string& message,
90                      ModelType model_type,
91                      ErrorType error_type) {
92   location_.reset(new tracked_objects::Location(location));
93   message_ = message;
94   model_type_ = model_type;
95   error_type_ = error_type;
96 }
97 
IsSet() const98 bool SyncError::IsSet() const {
99   return error_type_ != UNSET;
100 }
101 
102 
location() const103 const tracked_objects::Location& SyncError::location() const {
104   CHECK(IsSet());
105   return *location_;
106 }
107 
message() const108 const std::string& SyncError::message() const {
109   CHECK(IsSet());
110   return message_;
111 }
112 
model_type() const113 ModelType SyncError::model_type() const {
114   CHECK(IsSet());
115   return model_type_;
116 }
117 
error_type() const118 SyncError::ErrorType SyncError::error_type() const {
119   CHECK(IsSet());
120   return error_type_;
121 }
122 
ToString() const123 std::string SyncError::ToString() const {
124   if (!IsSet()) {
125     return std::string();
126   }
127   return location_->ToString() + ", " + ModelTypeToString(model_type_) +
128       " " + message_;
129 }
130 
PrintLogError() const131 void SyncError::PrintLogError() const {
132   LAZY_STREAM(logging::LogMessage(location_->file_name(),
133                                   location_->line_number(),
134                                   logging::LOG_ERROR).stream(),
135               LOG_IS_ON(ERROR))
136       << ModelTypeToString(model_type_) << " " << message_;
137 }
138 
PrintTo(const SyncError & sync_error,std::ostream * os)139 void PrintTo(const SyncError& sync_error, std::ostream* os) {
140   *os << sync_error.ToString();
141 }
142 
143 }  // namespace syncer
144