sync_error.cc 3.8 KB

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