| OLD | NEW |
| (Empty) |
| 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 "components/sync/api/sync_error.h" | |
| 6 | |
| 7 #include <ostream> | |
| 8 | |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 | |
| 12 namespace syncer { | |
| 13 | |
| 14 SyncError::SyncError() { | |
| 15 Clear(); | |
| 16 } | |
| 17 | |
| 18 SyncError::SyncError(const tracked_objects::Location& location, | |
| 19 ErrorType error_type, | |
| 20 const std::string& message, | |
| 21 ModelType model_type) { | |
| 22 DCHECK(error_type != UNSET); | |
| 23 Init(location, message, model_type, error_type); | |
| 24 PrintLogError(); | |
| 25 } | |
| 26 | |
| 27 SyncError::SyncError(const SyncError& other) { | |
| 28 Copy(other); | |
| 29 } | |
| 30 | |
| 31 SyncError::~SyncError() {} | |
| 32 | |
| 33 SyncError& SyncError::operator=(const SyncError& other) { | |
| 34 if (this == &other) { | |
| 35 return *this; | |
| 36 } | |
| 37 Copy(other); | |
| 38 return *this; | |
| 39 } | |
| 40 | |
| 41 void SyncError::Copy(const SyncError& other) { | |
| 42 if (other.IsSet()) { | |
| 43 Init(other.location(), other.message(), other.model_type(), | |
| 44 other.error_type()); | |
| 45 } else { | |
| 46 Clear(); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void SyncError::Clear() { | |
| 51 location_.reset(); | |
| 52 message_ = std::string(); | |
| 53 model_type_ = UNSPECIFIED; | |
| 54 error_type_ = UNSET; | |
| 55 } | |
| 56 | |
| 57 void SyncError::Reset(const tracked_objects::Location& location, | |
| 58 const std::string& message, | |
| 59 ModelType model_type) { | |
| 60 Init(location, message, model_type, DATATYPE_ERROR); | |
| 61 PrintLogError(); | |
| 62 } | |
| 63 | |
| 64 void SyncError::Init(const tracked_objects::Location& location, | |
| 65 const std::string& message, | |
| 66 ModelType model_type, | |
| 67 ErrorType error_type) { | |
| 68 location_.reset(new tracked_objects::Location(location)); | |
| 69 message_ = message; | |
| 70 model_type_ = model_type; | |
| 71 error_type_ = error_type; | |
| 72 } | |
| 73 | |
| 74 bool SyncError::IsSet() const { | |
| 75 return error_type_ != UNSET; | |
| 76 } | |
| 77 | |
| 78 const tracked_objects::Location& SyncError::location() const { | |
| 79 CHECK(IsSet()); | |
| 80 return *location_; | |
| 81 } | |
| 82 | |
| 83 const std::string& SyncError::message() const { | |
| 84 CHECK(IsSet()); | |
| 85 return message_; | |
| 86 } | |
| 87 | |
| 88 ModelType SyncError::model_type() const { | |
| 89 CHECK(IsSet()); | |
| 90 return model_type_; | |
| 91 } | |
| 92 | |
| 93 SyncError::ErrorType SyncError::error_type() const { | |
| 94 CHECK(IsSet()); | |
| 95 return error_type_; | |
| 96 } | |
| 97 | |
| 98 SyncError::Severity SyncError::GetSeverity() const { | |
| 99 switch (error_type_) { | |
| 100 case UNREADY_ERROR: | |
| 101 case DATATYPE_POLICY_ERROR: | |
| 102 return SYNC_ERROR_SEVERITY_INFO; | |
| 103 default: | |
| 104 return SYNC_ERROR_SEVERITY_ERROR; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 std::string SyncError::GetMessagePrefix() const { | |
| 109 std::string type_message; | |
| 110 switch (error_type_) { | |
| 111 case UNRECOVERABLE_ERROR: | |
| 112 type_message = "unrecoverable error was encountered: "; | |
| 113 break; | |
| 114 case DATATYPE_ERROR: | |
| 115 type_message = "datatype error was encountered: "; | |
| 116 break; | |
| 117 case PERSISTENCE_ERROR: | |
| 118 type_message = "persistence error was encountered: "; | |
| 119 break; | |
| 120 case CRYPTO_ERROR: | |
| 121 type_message = "cryptographer error was encountered: "; | |
| 122 break; | |
| 123 case UNREADY_ERROR: | |
| 124 type_message = "unready error was encountered: "; | |
| 125 break; | |
| 126 case DATATYPE_POLICY_ERROR: | |
| 127 type_message = "disabled due to configuration constraints: "; | |
| 128 break; | |
| 129 case UNSET: | |
| 130 NOTREACHED() << "Invalid error type"; | |
| 131 break; | |
| 132 } | |
| 133 return type_message; | |
| 134 } | |
| 135 | |
| 136 std::string SyncError::ToString() const { | |
| 137 if (!IsSet()) { | |
| 138 return std::string(); | |
| 139 } | |
| 140 return location_->ToString() + ", " + ModelTypeToString(model_type_) + " " + | |
| 141 GetMessagePrefix() + message_; | |
| 142 } | |
| 143 | |
| 144 void SyncError::PrintLogError() const { | |
| 145 logging::LogSeverity logSeverity = (GetSeverity() == SYNC_ERROR_SEVERITY_INFO) | |
| 146 ? logging::LOG_VERBOSE | |
| 147 : logging::LOG_ERROR; | |
| 148 | |
| 149 LAZY_STREAM(logging::LogMessage(location_->file_name(), | |
| 150 location_->line_number(), logSeverity) | |
| 151 .stream(), | |
| 152 logSeverity >= ::logging::GetMinLogLevel()) | |
| 153 << ModelTypeToString(model_type_) << " " << GetMessagePrefix() | |
| 154 << message_; | |
| 155 } | |
| 156 | |
| 157 void PrintTo(const SyncError& sync_error, std::ostream* os) { | |
| 158 *os << sync_error.ToString(); | |
| 159 } | |
| 160 | |
| 161 } // namespace syncer | |
| OLD | NEW |