Home | History | Annotate | Download | only in sql
      1 // Copyright (c) 2010 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 APP_SQL_DIAGNOSTIC_ERROR_DELEGATE_H_
      6 #define APP_SQL_DIAGNOSTIC_ERROR_DELEGATE_H_
      7 #pragma once
      8 
      9 #include "app/sql/connection.h"
     10 #include "base/logging.h"
     11 #include "base/metrics/histogram.h"
     12 
     13 namespace sql {
     14 
     15 // This class handles the exceptional sqlite errors that we might encounter
     16 // if for example the db is corrupted. Right now we just generate a UMA
     17 // histogram for release and an assert for debug builds.
     18 //
     19 // Why is it a template you ask? well, that is a funny story. The histograms
     20 // need to be singletons that is why they are always static at the function
     21 // scope, but we cannot use the Singleton class because they are not default
     22 // constructible. The template parameter makes the compiler to create unique
     23 // classes that don't share the same static variable.
     24 template <class UniqueT>
     25 class DiagnosticErrorDelegate : public ErrorDelegate {
     26  public:
     27 
     28   virtual int OnError(int error, Connection* connection,
     29                       Statement* stmt) {
     30     NOTREACHED() << "sqlite error " << error
     31                  << ", errno " << connection->GetLastErrno()
     32                  << ": " << connection->GetErrorMessage();
     33     RecordErrorInHistogram(error);
     34     return error;
     35   }
     36 
     37  private:
     38   static void RecordErrorInHistogram(int error) {
     39     // Trim off the extended error codes.
     40     error &= 0xff;
     41 
     42     // The histogram values from sqlite result codes go currently from 1 to
     43     // 26 currently but 50 gives them room to grow.
     44     UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50);
     45   }
     46 };
     47 
     48 }  // namespace sql
     49 
     50 #endif  // APP_SQL_DIAGNOSTIC_ERROR_DELEGATE_H_
     51