sync_error.h revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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
5#ifndef SYNC_API_SYNC_ERROR_H_
6#define SYNC_API_SYNC_ERROR_H_
7
8#include <iosfwd>
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "sync/base/sync_export.h"
13#include "sync/internal_api/public/base/model_type.h"
14
15namespace tracked_objects {
16class Location;
17}  // namespace tracked_objects
18
19namespace syncer {
20
21// Sync errors are used for debug purposes and handled internally and/or
22// exposed through Chrome's "about:sync" internal page.
23// This class is copy-friendly and thread-safe.
24class SYNC_EXPORT SyncError {
25 public:
26  // Error types are used to distinguish general datatype errors (which result
27  // in the datatype being disabled) from actionable sync errors (which might
28  // have more complicated results).
29  enum ErrorType {
30    UNSET,                // No error.
31    UNRECOVERABLE_ERROR,  // An unrecoverable runtime error was encountered, and
32                          // sync should be disabled and purged completely.
33    DATATYPE_ERROR,       // A datatype error was encountered, and the datatype
34                          // should be disabled and purged completely. Note that
35                          // datatype errors may be reset, triggering a
36                          // re-enable.
37    PERSISTENCE_ERROR,    // A persistence error was detected, and the
38                          // datataype should be associated after a sync update.
39    CRYPTO_ERROR,         // A cryptographer error was detected, and the
40                          // datatype should be associated after it is resolved.
41    UNREADY_ERROR,        // The type is not ready to start yet, so should be
42                          // neither purged nor enabled until it is ready.
43  };
44
45  // Default constructor refers to "no error", and IsSet() will return false.
46  SyncError();
47
48  // Create a new Sync error of type |error_type| triggered by |model_type|
49  // from the specified location. IsSet() will return true afterward. Will
50  // create and print an error specific message to LOG(ERROR).
51  SyncError(const tracked_objects::Location& location,
52            ErrorType error_type,
53            const std::string& message,
54            ModelType model_type);
55
56  // Copy and assign via deep copy.
57  SyncError(const SyncError& other);
58  SyncError& operator=(const SyncError& other);
59
60  ~SyncError();
61
62  // Reset the current error to a new datatype error. May be called
63  // irrespective of whether IsSet() is true. After this is called, IsSet()
64  // will return true.
65  // Will print the new error to LOG(ERROR).
66  void Reset(const tracked_objects::Location& location,
67             const std::string& message,
68             ModelType type);
69
70  // Whether this is a valid error or not.
71  bool IsSet() const;
72
73  // These must only be called if IsSet() is true.
74  const tracked_objects::Location& location() const;
75  const std::string& message() const;
76  ModelType model_type() const;
77  ErrorType error_type() const;
78
79  // Returns empty string is IsSet() is false.
80  std::string ToString() const;
81 private:
82  // Print error information to log.
83  void PrintLogError() const;
84
85  // Make a copy of a SyncError. If other.IsSet() == false, this->IsSet() will
86  // now return false.
87  void Copy(const SyncError& other);
88
89  // Initialize the local error data with the specified error data. After this
90  // is called, IsSet() will return true.
91  void Init(const tracked_objects::Location& location,
92            const std::string& message,
93            ModelType model_type,
94            ErrorType error_type);
95
96  // Reset the error to it's default (unset) values.
97  void Clear();
98
99  // scoped_ptr is necessary because Location objects aren't assignable.
100  scoped_ptr<tracked_objects::Location> location_;
101  std::string message_;
102  ModelType model_type_;
103  ErrorType error_type_;
104};
105
106// gmock printer helper.
107SYNC_EXPORT void PrintTo(const SyncError& sync_error, std::ostream* os);
108
109}  // namespace syncer
110
111#endif  // SYNC_API_SYNC_ERROR_H_
112