- add sources.
[platform/framework/web/crosswalk.git] / src / sync / api / sync_error.cc
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
15 SyncError::SyncError() {
16   Clear();
17 }
18
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     default:
38       NOTREACHED();
39       type_message = "invalid error: ";
40   }
41   Init(location, type_message + custom_message, model_type, error_type);
42   PrintLogError();
43 }
44
45 SyncError::SyncError(const SyncError& other) {
46   Copy(other);
47 }
48
49 SyncError::~SyncError() {
50 }
51
52 SyncError& SyncError::operator=(const SyncError& other) {
53   if (this == &other) {
54     return *this;
55   }
56   Copy(other);
57   return *this;
58 }
59
60 void SyncError::Copy(const SyncError& other) {
61   if (other.IsSet()) {
62     Init(other.location(),
63          other.message(),
64          other.model_type(),
65          other.error_type());
66   } else {
67     Clear();
68   }
69 }
70
71 void SyncError::Clear() {
72   location_.reset();
73   message_ = std::string();
74   model_type_ = UNSPECIFIED;
75   error_type_ = UNSET;
76 }
77
78 void SyncError::Reset(const tracked_objects::Location& location,
79                       const std::string& message,
80                       ModelType model_type) {
81   Init(location, message, model_type, DATATYPE_ERROR);
82   PrintLogError();
83 }
84
85 void SyncError::Init(const tracked_objects::Location& location,
86                      const std::string& message,
87                      ModelType model_type,
88                      ErrorType error_type) {
89   location_.reset(new tracked_objects::Location(location));
90   message_ = message;
91   model_type_ = model_type;
92   error_type_ = error_type;
93 }
94
95 bool SyncError::IsSet() const {
96   return error_type_ != UNSET;
97 }
98
99
100 const tracked_objects::Location& SyncError::location() const {
101   CHECK(IsSet());
102   return *location_;
103 }
104
105 const std::string& SyncError::message() const {
106   CHECK(IsSet());
107   return message_;
108 }
109
110 ModelType SyncError::model_type() const {
111   CHECK(IsSet());
112   return model_type_;
113 }
114
115 SyncError::ErrorType SyncError::error_type() const {
116   CHECK(IsSet());
117   return error_type_;
118 }
119
120 std::string SyncError::ToString() const {
121   if (!IsSet()) {
122     return std::string();
123   }
124   return location_->ToString() + ", " + ModelTypeToString(model_type_) +
125       " " + message_;
126 }
127
128 void SyncError::PrintLogError() const {
129   LAZY_STREAM(logging::LogMessage(location_->file_name(),
130                                   location_->line_number(),
131                                   logging::LOG_ERROR).stream(),
132               LOG_IS_ON(ERROR))
133       << ModelTypeToString(model_type_) << " " << message_;
134 }
135
136 void PrintTo(const SyncError& sync_error, std::ostream* os) {
137   *os << sync_error.ToString();
138 }
139
140 }  // namespace syncer