mojo: Add deserialization to python structs.
[chromium-blink-merge.git] / sync / api / sync_error.cc
blob666be5f68fd7b17f2056079b3f909bc3dec25359
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.
5 #include "sync/api/sync_error.h"
7 #include <ostream>
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "sync/internal_api/public/base/model_type.h"
13 namespace syncer {
15 SyncError::SyncError() {
16 Clear();
19 SyncError::SyncError(const tracked_objects::Location& location,
20 ErrorType error_type,
21 const std::string& message,
22 ModelType model_type) {
23 DCHECK(error_type != UNSET);
24 Init(location, message, model_type, error_type);
25 PrintLogError();
28 SyncError::SyncError(const SyncError& other) {
29 Copy(other);
32 SyncError::~SyncError() {
35 SyncError& SyncError::operator=(const SyncError& other) {
36 if (this == &other) {
37 return *this;
39 Copy(other);
40 return *this;
43 void SyncError::Copy(const SyncError& other) {
44 if (other.IsSet()) {
45 Init(other.location(),
46 other.message(),
47 other.model_type(),
48 other.error_type());
49 } else {
50 Clear();
54 void SyncError::Clear() {
55 location_.reset();
56 message_ = std::string();
57 model_type_ = UNSPECIFIED;
58 error_type_ = UNSET;
61 void SyncError::Reset(const tracked_objects::Location& location,
62 const std::string& message,
63 ModelType model_type) {
64 Init(location, message, model_type, DATATYPE_ERROR);
65 PrintLogError();
68 void SyncError::Init(const tracked_objects::Location& location,
69 const std::string& message,
70 ModelType model_type,
71 ErrorType error_type) {
72 location_.reset(new tracked_objects::Location(location));
73 message_ = message;
74 model_type_ = model_type;
75 error_type_ = error_type;
78 bool SyncError::IsSet() const {
79 return error_type_ != UNSET;
83 const tracked_objects::Location& SyncError::location() const {
84 CHECK(IsSet());
85 return *location_;
88 const std::string& SyncError::message() const {
89 CHECK(IsSet());
90 return message_;
93 ModelType SyncError::model_type() const {
94 CHECK(IsSet());
95 return model_type_;
98 SyncError::ErrorType SyncError::error_type() const {
99 CHECK(IsSet());
100 return error_type_;
103 SyncError::Severity SyncError::GetSeverity() const {
104 switch (error_type_) {
105 case UNREADY_ERROR:
106 case DATATYPE_POLICY_ERROR:
107 return SYNC_ERROR_SEVERITY_INFO;
108 default:
109 return SYNC_ERROR_SEVERITY_ERROR;
113 std::string SyncError::GetMessagePrefix() const {
114 std::string type_message;
115 switch (error_type_) {
116 case UNRECOVERABLE_ERROR:
117 type_message = "unrecoverable error was encountered: ";
118 break;
119 case DATATYPE_ERROR:
120 type_message = "datatype error was encountered: ";
121 break;
122 case PERSISTENCE_ERROR:
123 type_message = "persistence error was encountered: ";
124 break;
125 case CRYPTO_ERROR:
126 type_message = "cryptographer error was encountered: ";
127 break;
128 case UNREADY_ERROR:
129 type_message = "unready error was encountered: ";
130 break;
131 case DATATYPE_POLICY_ERROR:
132 type_message = "disabled due to configuration constraints: ";
133 break;
134 case UNSET:
135 NOTREACHED() << "Invalid error type";
136 break;
138 return type_message;
141 std::string SyncError::ToString() const {
142 if (!IsSet()) {
143 return std::string();
145 return location_->ToString() + ", " + ModelTypeToString(model_type_) +
146 " " + GetMessagePrefix() + message_;
149 void SyncError::PrintLogError() const {
150 logging::LogSeverity logSeverity =
151 (GetSeverity() == SYNC_ERROR_SEVERITY_INFO)
152 ? logging::LOG_VERBOSE : logging::LOG_ERROR;
154 LAZY_STREAM(logging::LogMessage(location_->file_name(),
155 location_->line_number(),
156 logSeverity).stream(),
157 logSeverity >= ::logging::GetMinLogLevel())
158 << ModelTypeToString(model_type_) << " "
159 << GetMessagePrefix() << message_;
162 void PrintTo(const SyncError& sync_error, std::ostream* os) {
163 *os << sync_error.ToString();
166 } // namespace syncer