sync_error.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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 13namespace syncer { 14 15SyncError::SyncError() { 16 Clear(); 17} 18 19SyncError::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 48SyncError::SyncError(const SyncError& other) { 49 Copy(other); 50} 51 52SyncError::~SyncError() { 53} 54 55SyncError& SyncError::operator=(const SyncError& other) { 56 if (this == &other) { 57 return *this; 58 } 59 Copy(other); 60 return *this; 61} 62 63void 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 74void SyncError::Clear() { 75 location_.reset(); 76 message_ = std::string(); 77 model_type_ = UNSPECIFIED; 78 error_type_ = UNSET; 79} 80 81void 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 88void 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 98bool SyncError::IsSet() const { 99 return error_type_ != UNSET; 100} 101 102 103const tracked_objects::Location& SyncError::location() const { 104 CHECK(IsSet()); 105 return *location_; 106} 107 108const std::string& SyncError::message() const { 109 CHECK(IsSet()); 110 return message_; 111} 112 113ModelType SyncError::model_type() const { 114 CHECK(IsSet()); 115 return model_type_; 116} 117 118SyncError::ErrorType SyncError::error_type() const { 119 CHECK(IsSet()); 120 return error_type_; 121} 122 123std::string SyncError::ToString() const { 124 if (!IsSet()) { 125 return std::string(); 126 } 127 return location_->ToString() + ", " + ModelTypeToString(model_type_) + 128 " " + message_; 129} 130 131void 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 139void PrintTo(const SyncError& sync_error, std::ostream* os) { 140 *os << sync_error.ToString(); 141} 142 143} // namespace syncer 144