blob: 6b90ccf8b4f16738f24992e002d2c755f55fda35 [file] [log] [blame]
[email protected]0d04ede2012-10-18 04:31:531// Copyright (c) 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 SQL_ERROR_DELEGATE_UTIL_H_
6#define SQL_ERROR_DELEGATE_UTIL_H_
7
8#include "base/metrics/histogram.h"
9#include "sql/connection.h"
10#include "sql/sql_export.h"
11
12namespace sql {
13
14// Returns true if it is highly unlikely that the database can recover from
15// |error|.
16SQL_EXPORT bool IsErrorCatastrophic(int error);
17
18// Log error in console in debug mode and generate a UMA histogram in release
19// mode for |error| for |UniqueT::name()|.
20// This function is templated because histograms need to be singletons. That is
21// why they are always static at the function scope. The template parameter
22// makes the compiler create unique functions that don't share the same static
23// variable.
24template <class UniqueT>
25void LogAndRecordErrorInHistogram(int error,
26 sql::Connection* connection) {
27 LOG(ERROR) << "sqlite error " << error
28 << ", errno " << connection->GetLastErrno()
29 << ": " << connection->GetErrorMessage();
30
31 // Trim off the extended error codes.
32 error &= 0xff;
33
34 // The histogram values from sqlite result codes currently go from 1 to 26
35 // but 50 gives them room to grow.
36 UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50);
37}
38
39} // namespace sql
40
41#endif // SQL_ERROR_DELEGATE_UTIL_H_