aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/error.cxx
blob: d34f55b73a35cfd231257478034d3b2c7ae98daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// file      : odb/sqlite/errors.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <sqlite3.h>

#include <new>    // std::bad_alloc
#include <string>

#include <odb/sqlite/connection.hxx>
#include <odb/sqlite/exceptions.hxx>

using namespace std;

namespace odb
{
  namespace sqlite
  {
    void
    translate_error (int e, connection& c)
    {
      sqlite3* h (c.handle ());
      int ee (sqlite3_extended_errcode (h));
      string m;

      switch (e)
      {
      case SQLITE_NOMEM:
        {
          throw bad_alloc ();
        }
      case SQLITE_MISUSE:
        {
          // In case of SQLITE_MISUSE, error code/message may or may not
          // be set.
          //
          ee = e;
          m = "SQLite API misuse";
          break;
        }
      case SQLITE_BUSY:
      case SQLITE_LOCKED:
      case SQLITE_IOERR:
        {
          if (e != SQLITE_IOERR || ee == SQLITE_IOERR_BLOCKED)
            throw deadlock ();

          // Fall throught.
        }
      default:
        {
          m = sqlite3_errmsg (h);
          break;
        }
      }

      throw database_exception (e, ee, m);
    }
  }
}