diff options
author | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-18 04:31:53 +0000 |
---|---|---|
committer | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-18 04:31:53 +0000 |
commit | 0d04ede3f14d8c248b5f91687694673df8c145b5 (patch) | |
tree | 3c2ea2c08726dd0424cb295c790308280289c890 /sql | |
parent | b50b6edf3dee383cae64c39e4b101c848b7ebc74 (diff) | |
download | chromium_src-0d04ede3f14d8c248b5f91687694673df8c145b5.zip chromium_src-0d04ede3f14d8c248b5f91687694673df8c145b5.tar.gz chromium_src-0d04ede3f14d8c248b5f91687694673df8c145b5.tar.bz2 |
Move ErrorDelegate to its own file and add static utility functions to ErrorDelegate
BUG=151841
Test=None
R=shess, erikwright
TBR=cpu
Review URL: https://chromiumcodereview.appspot.com/11141012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162647 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r-- | sql/connection.h | 14 | ||||
-rw-r--r-- | sql/diagnostic_error_delegate.h | 26 | ||||
-rw-r--r-- | sql/error_delegate_util.cc | 80 | ||||
-rw-r--r-- | sql/error_delegate_util.h | 41 | ||||
-rw-r--r-- | sql/sql.gyp | 2 |
5 files changed, 135 insertions, 28 deletions
diff --git a/sql/connection.h b/sql/connection.h index 1aef064..319517e 100644 --- a/sql/connection.h +++ b/sql/connection.h @@ -83,17 +83,17 @@ class SQL_EXPORT ErrorDelegate { public: virtual ~ErrorDelegate(); - // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h - // |connection| is db connection where the error happened and |stmt| is - // our best guess at the statement that triggered the error. Do not store - // these pointers. + // |error| is an sqlite result code as seen in sqlite3.h. |connection| is the + // db connection where the error happened and |stmt| is our best guess at the + // statement that triggered the error. Do not store these pointers. // // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on // initialization). // - // If the error condition has been fixed an the original statement succesfuly - // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended - // that you return the original |error| or the appropiae error code. + // If the error condition has been fixed and the original statement succesfuly + // re-tried then returning SQLITE_OK is appropriate; otherwise it is + // recommended that you return the original |error| or the appropriate error + // code. virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; }; diff --git a/sql/diagnostic_error_delegate.h b/sql/diagnostic_error_delegate.h index 4b8ce32..78b3d9d 100644 --- a/sql/diagnostic_error_delegate.h +++ b/sql/diagnostic_error_delegate.h @@ -6,8 +6,8 @@ #define SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ #include "base/logging.h" -#include "base/metrics/histogram.h" #include "sql/connection.h" +#include "sql/error_delegate_util.h" #include "sql/sql_export.h" namespace sql { @@ -15,12 +15,8 @@ namespace sql { // This class handles the exceptional sqlite errors that we might encounter // if for example the db is corrupted. Right now we just generate a UMA // histogram for release and an assert for debug builds. -// -// Why is it a template you ask? well, that is a funny story. The histograms -// need to be singletons that is why they are always static at the function -// scope, but we cannot use the Singleton class because they are not default -// constructible. The template parameter makes the compiler to create unique -// classes that don't share the same static variable. +// See error_delegate_util.h for an explanation as to why this class is a +// template. template <class UniqueT> class DiagnosticErrorDelegate : public ErrorDelegate { public: @@ -29,24 +25,12 @@ class DiagnosticErrorDelegate : public ErrorDelegate { virtual int OnError(int error, Connection* connection, Statement* stmt) { - LOG(ERROR) << "sqlite error " << error - << ", errno " << connection->GetLastErrno() - << ": " << connection->GetErrorMessage(); - RecordErrorInHistogram(error); + LogAndRecordErrorInHistogram<UniqueT>(error, connection); return error; } private: - static void RecordErrorInHistogram(int error) { - // Trim off the extended error codes. - error &= 0xff; - - // The histogram values from sqlite result codes go currently from 1 to - // 26 currently but 50 gives them room to grow. - UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50); - } - - DISALLOW_COPY_AND_ASSIGN(DiagnosticErrorDelegate); + DISALLOW_COPY_AND_ASSIGN(DiagnosticErrorDelegate); }; } // namespace sql diff --git a/sql/error_delegate_util.cc b/sql/error_delegate_util.cc new file mode 100644 index 0000000..37fe006 --- /dev/null +++ b/sql/error_delegate_util.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sql/error_delegate_util.h" + +#include "third_party/sqlite/sqlite3.h" + +namespace sql { + +bool IsErrorCatastrophic(int error) { + switch (error) { + case SQLITE_DONE: + case SQLITE_OK: + // Theoretically, the wrapped delegate might have resolved the error, and + // we would end up here. + return false; + + case SQLITE_CORRUPT: + case SQLITE_NOTADB: + // Highly unlikely we would ever recover from these. + return true; + + case SQLITE_CANTOPEN: + // TODO(erikwright): Figure out what this means. + return false; + + case SQLITE_IOERR: + // This could be broken blocks, in which case deleting the DB would be a + // good idea. But it might also be transient. + // TODO(erikwright): Figure out if we can distinguish between the two, + // or determine through metrics analysis to what extent these failures are + // transient. + return false; + + case SQLITE_BUSY: + // Presumably transient. + return false; + + case SQLITE_TOOBIG: + case SQLITE_FULL: + case SQLITE_NOMEM: + // Not a problem with the database. + return false; + + case SQLITE_READONLY: + // Presumably either transient or we don't have the privileges to + // move/delete the file anyway. + return false; + + case SQLITE_CONSTRAINT: + case SQLITE_ERROR: + // These probgably indicate a programming error or a migration failure + // that we prefer not to mask. + return false; + + case SQLITE_LOCKED: + case SQLITE_INTERNAL: + case SQLITE_PERM: + case SQLITE_ABORT: + case SQLITE_INTERRUPT: + case SQLITE_NOTFOUND: + case SQLITE_PROTOCOL: + case SQLITE_EMPTY: + case SQLITE_SCHEMA: + case SQLITE_MISMATCH: + case SQLITE_MISUSE: + case SQLITE_NOLFS: + case SQLITE_AUTH: + case SQLITE_FORMAT: + case SQLITE_RANGE: + case SQLITE_ROW: + // None of these appear in error reports, so for now let's not try to + // guess at how to handle them. + return false; + } + return false; +} + +} // namespace sql diff --git a/sql/error_delegate_util.h b/sql/error_delegate_util.h new file mode 100644 index 0000000..6b90ccf --- /dev/null +++ b/sql/error_delegate_util.h @@ -0,0 +1,41 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SQL_ERROR_DELEGATE_UTIL_H_ +#define SQL_ERROR_DELEGATE_UTIL_H_ + +#include "base/metrics/histogram.h" +#include "sql/connection.h" +#include "sql/sql_export.h" + +namespace sql { + +// Returns true if it is highly unlikely that the database can recover from +// |error|. +SQL_EXPORT bool IsErrorCatastrophic(int error); + +// Log error in console in debug mode and generate a UMA histogram in release +// mode for |error| for |UniqueT::name()|. +// This function is templated because histograms need to be singletons. That is +// why they are always static at the function scope. The template parameter +// makes the compiler create unique functions that don't share the same static +// variable. +template <class UniqueT> +void LogAndRecordErrorInHistogram(int error, + sql::Connection* connection) { + LOG(ERROR) << "sqlite error " << error + << ", errno " << connection->GetLastErrno() + << ": " << connection->GetErrorMessage(); + + // Trim off the extended error codes. + error &= 0xff; + + // The histogram values from sqlite result codes currently go from 1 to 26 + // but 50 gives them room to grow. + UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50); +} + +} // namespace sql + +#endif // SQL_ERROR_DELEGATE_UTIL_H_ diff --git a/sql/sql.gyp b/sql/sql.gyp index 72d361c..86962b3 100644 --- a/sql/sql.gyp +++ b/sql/sql.gyp @@ -19,6 +19,8 @@ 'connection.cc', 'connection.h', 'diagnostic_error_delegate.h', + 'error_delegate_util.cc', + 'error_delegate_util.h', 'init_status.h', 'meta_table.cc', 'meta_table.h', |