sync_error.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. They are considered
23// unrecoverable for the datatype creating them, and should only be used as
24// such.
25// This class is copy-friendly and thread-safe.
26class SYNC_EXPORT SyncError {
27 public:
28  // Default constructor refers to "no error", and IsSet() will return false.
29  SyncError();
30
31  // Create a new Sync error triggered by datatype |type| with debug message
32  // |message| from the specified location. IsSet() will return true.
33  // Will print the new error to LOG(ERROR).
34  SyncError(const tracked_objects::Location& location,
35            const std::string& message,
36            ModelType type);
37
38  // Copy and assign via deep copy.
39  SyncError(const SyncError& other);
40  SyncError& operator=(const SyncError& other);
41
42  ~SyncError();
43
44  // Reset the current error to a new error. May be called irrespective of
45  // whether IsSet() is true. After this is called, IsSet() will return true.
46  // Will print the new error to LOG(ERROR).
47  void Reset(const tracked_objects::Location& location,
48             const std::string& message,
49             ModelType type);
50
51  // Whether this is a valid error or not.
52  bool IsSet() const;
53
54  // These must only be called if IsSet() is true.
55  const tracked_objects::Location& location() const;
56  const std::string& message() const;
57  ModelType type() const;
58
59  // Returns empty string is IsSet() is false.
60  std::string ToString() const;
61 private:
62  // Print error information to log.
63  void PrintLogError() const;
64
65  // Make a copy of a SyncError. If other.IsSet() == false, this->IsSet() will
66  // now return false.
67  void Copy(const SyncError& other);
68
69  // Initialize the local error data with the specified error data. After this
70  // is called, IsSet() will return true.
71  void Init(const tracked_objects::Location& location,
72            const std::string& message,
73            ModelType type);
74
75  // Reset the error to it's default (unset) values.
76  void Clear();
77
78  // scoped_ptr is necessary because Location objects aren't assignable.
79  scoped_ptr<tracked_objects::Location> location_;
80  std::string message_;
81  ModelType type_;
82};
83
84// gmock printer helper.
85SYNC_EXPORT void PrintTo(const SyncError& sync_error, std::ostream* os);
86
87}  // namespace syncer
88
89#endif  // SYNC_API_SYNC_ERROR_H_
90