sync_error.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 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. #ifndef COMPONENTS_SYNC_MODEL_SYNC_ERROR_H_
  5. #define COMPONENTS_SYNC_MODEL_SYNC_ERROR_H_
  6. #include <memory>
  7. #include <string>
  8. #include "components/sync/base/model_type.h"
  9. namespace base {
  10. class Location;
  11. } // namespace base
  12. namespace syncer {
  13. // Sync errors are used for debug purposes and handled internally and/or
  14. // exposed through Chrome's "chrome://sync-internals" internal page.
  15. // This class is copy-friendly and thread-safe.
  16. class SyncError {
  17. public:
  18. // Error types are used to distinguish general datatype errors (which result
  19. // in the datatype being disabled) from actionable sync errors (which might
  20. // have more complicated results).
  21. enum ErrorType {
  22. UNSET, // No error.
  23. DATATYPE_ERROR, // A datatype error was encountered, and the datatype
  24. // should be disabled and purged completely. Note
  25. // that datatype errors may be reset, triggering a
  26. // re-enable.
  27. CRYPTO_ERROR, // A cryptographer error was detected, and the
  28. // datatype should be associated after it is
  29. // resolved.
  30. UNREADY_ERROR, // A datatype is not ready to start yet, so should be
  31. // neither purged nor enabled until it is ready.
  32. DATATYPE_POLICY_ERROR // A datatype should be disabled and purged due to
  33. // configuration constraints.
  34. };
  35. // Severity is used to indicate how an error should be logged and
  36. // represented to an end user.
  37. enum Severity {
  38. SYNC_ERROR_SEVERITY_ERROR, // Severe unrecoverable error.
  39. SYNC_ERROR_SEVERITY_INFO // Low-severity recoverable error or
  40. // configuration policy issue.
  41. };
  42. // Default constructor refers to "no error", and IsSet() will return false.
  43. SyncError();
  44. // Create a new Sync error of type |error_type| triggered by |model_type|
  45. // from the specified location. IsSet() will return true afterward. Will
  46. // create and print an error specific message to LOG(ERROR).
  47. SyncError(const base::Location& location,
  48. ErrorType error_type,
  49. const std::string& message,
  50. ModelType model_type);
  51. // Copy and assign via deep copy.
  52. SyncError(const SyncError& other);
  53. SyncError& operator=(const SyncError& other);
  54. ~SyncError();
  55. // Reset the current error to a new datatype error. May be called
  56. // irrespective of whether IsSet() is true. After this is called, IsSet()
  57. // will return true.
  58. // Will print the new error to LOG(ERROR).
  59. void Reset(const base::Location& location,
  60. const std::string& message,
  61. ModelType type);
  62. // Whether this is a valid error or not.
  63. bool IsSet() const;
  64. // These must only be called if IsSet() is true.
  65. const base::Location& location() const;
  66. const std::string& message() const;
  67. ModelType model_type() const;
  68. ErrorType error_type() const;
  69. // Error severity for logging and UI purposes.
  70. Severity GetSeverity() const;
  71. // Type specific message prefix for logging and UI purposes.
  72. std::string GetMessagePrefix() const;
  73. // Returns empty string is IsSet() is false.
  74. std::string ToString() const;
  75. private:
  76. // Print error information to log.
  77. void PrintLogError() const;
  78. // Make a copy of a SyncError. If other.IsSet() == false, this->IsSet() will
  79. // now return false.
  80. void Copy(const SyncError& other);
  81. // Initialize the local error data with the specified error data. After this
  82. // is called, IsSet() will return true.
  83. void Init(const base::Location& location,
  84. const std::string& message,
  85. ModelType model_type,
  86. ErrorType error_type);
  87. // Reset the error to it's default (unset) values.
  88. void Clear();
  89. // unique_ptr is necessary because Location objects aren't assignable.
  90. std::unique_ptr<base::Location> location_;
  91. std::string message_;
  92. ModelType model_type_;
  93. ErrorType error_type_;
  94. };
  95. } // namespace syncer
  96. #endif // COMPONENTS_SYNC_MODEL_SYNC_ERROR_H_