scoped_error_ignorer.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1// Copyright 2013 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 SQL_TEST_SCOPED_ERROR_IGNORER_H_
6#define SQL_TEST_SCOPED_ERROR_IGNORER_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "sql/connection.h"
12
13namespace sql {
14
15// sql::Connection and sql::Statement treat most SQLite errors as
16// fatal in debug mode.  The intention is to catch inappropriate uses
17// of SQL before the code is shipped to production.  This makes it
18// challenging to write tests for things like recovery from
19// corruption.  This scoper can be used to ignore selected errors
20// during a test.  Errors are ignored globally (on all connections).
21//
22// Since errors can be very context-dependent, the class is pedantic -
23// specific errors must be ignored, and every error ignored must be
24// seen.
25//
26// NOTE(shess): There are still fatal error cases this does not
27// address.  If your test is handling database errors and you're
28// hitting a case not handled, contact me.
29class ScopedErrorIgnorer {
30 public:
31  ScopedErrorIgnorer();
32  ~ScopedErrorIgnorer();
33
34  // Add an error to ignore.  Extended error codes can be ignored
35  // specifically, or the base code can ignore an entire group
36  // (SQLITE_IOERR_* versus SQLITE_IOERR).
37  void IgnoreError(int err);
38
39  // Allow containing test to check if the errors were encountered.
40  // Failure to call results in ADD_FAILURE() in destructor.
41  bool CheckIgnoredErrors();
42
43  // Record an error and check if it should be ignored.
44  bool ShouldIgnore(int err);
45
46 private:
47  // Storage for callback passed to Connection::SetErrorIgnorer().
48  Connection::ErrorIgnorerCallback callback_;
49
50  // Record whether CheckIgnoredErrors() has been called.
51  bool checked_;
52
53  // Errors to ignore.
54  std::set<int> ignore_errors_;
55
56  // Errors which have been ignored.
57  std::set<int> errors_ignored_;
58
59  DISALLOW_COPY_AND_ASSIGN(ScopedErrorIgnorer);
60};
61
62}  // namespace sql
63
64#endif  // SQL_TEST_SCOPED_ERROR_IGNORER_H_
65