1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | #ident "$Id$" |
3 | /*====== |
4 | This file is part of PerconaFT. |
5 | |
6 | |
7 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
8 | |
9 | PerconaFT is free software: you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License, version 2, |
11 | as published by the Free Software Foundation. |
12 | |
13 | PerconaFT is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
20 | |
21 | ---------------------------------------- |
22 | |
23 | PerconaFT is free software: you can redistribute it and/or modify |
24 | it under the terms of the GNU Affero General Public License, version 3, |
25 | as published by the Free Software Foundation. |
26 | |
27 | PerconaFT is distributed in the hope that it will be useful, |
28 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30 | GNU Affero General Public License for more details. |
31 | |
32 | You should have received a copy of the GNU Affero General Public License |
33 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
34 | ======= */ |
35 | |
36 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
37 | |
38 | #pragma once |
39 | |
40 | #include <exception> |
41 | #include <string.h> |
42 | |
43 | #include <db.h> |
44 | |
45 | namespace ftcxx { |
46 | |
47 | class ft_exception : public std::exception { |
48 | int _code; |
49 | |
50 | static const char *ft_strerror(int code) { |
51 | switch (code) { |
52 | case DB_RUNRECOVERY: |
53 | return "DB_RUNRECOVERY" ; |
54 | case DB_KEYEXIST: |
55 | return "DB_KEYEXIST" ; |
56 | case DB_LOCK_DEADLOCK: |
57 | return "DB_LOCK_DEADLOCK" ; |
58 | case DB_LOCK_NOTGRANTED: |
59 | return "DB_LOCK_NOTGRANTED" ; |
60 | case DB_NOTFOUND: |
61 | return "DB_NOTFOUND" ; |
62 | case DB_SECONDARY_BAD: |
63 | return "DB_SECONDARY_BAD" ; |
64 | case DB_DONOTINDEX: |
65 | return "DB_DONOTINDEX" ; |
66 | case DB_BUFFER_SMALL: |
67 | return "DB_BUFFER_SMALL" ; |
68 | case DB_BADFORMAT: |
69 | return "DB_BADFORMAT" ; |
70 | case TOKUDB_OUT_OF_LOCKS: |
71 | return "TOKUDB_OUT_OF_LOCKS" ; |
72 | case TOKUDB_SUCCEEDED_EARLY: |
73 | return "TOKUDB_SUCCEEDED_EARLY" ; |
74 | case TOKUDB_FOUND_BUT_REJECTED: |
75 | return "TOKUDB_FOUND_BUT_REJECTED" ; |
76 | case TOKUDB_USER_CALLBACK_ERROR: |
77 | return "TOKUDB_USER_CALLBACK_ERROR" ; |
78 | case TOKUDB_DICTIONARY_TOO_OLD: |
79 | return "TOKUDB_DICTIONARY_TOO_OLD" ; |
80 | case TOKUDB_DICTIONARY_TOO_NEW: |
81 | return "TOKUDB_DICTIONARY_TOO_NEW" ; |
82 | case TOKUDB_DICTIONARY_NO_HEADER: |
83 | return "TOKUDB_DICTIONARY_NO_HEADER" ; |
84 | case TOKUDB_CANCELED: |
85 | return "TOKUDB_CANCELED" ; |
86 | case TOKUDB_NO_DATA: |
87 | return "TOKUDB_NO_DATA" ; |
88 | case TOKUDB_ACCEPT: |
89 | return "TOKUDB_ACCEPT" ; |
90 | case TOKUDB_MVCC_DICTIONARY_TOO_NEW: |
91 | return "TOKUDB_MVCC_DICTIONARY_TOO_NEW" ; |
92 | case TOKUDB_UPGRADE_FAILURE: |
93 | return "TOKUDB_UPGRADE_FAILURE" ; |
94 | case TOKUDB_TRY_AGAIN: |
95 | return "TOKUDB_TRY_AGAIN" ; |
96 | case TOKUDB_NEEDS_REPAIR: |
97 | return "TOKUDB_NEEDS_REPAIR" ; |
98 | case TOKUDB_CURSOR_CONTINUE: |
99 | return "TOKUDB_CURSOR_CONTINUE" ; |
100 | case TOKUDB_BAD_CHECKSUM: |
101 | return "TOKUDB_BAD_CHECKSUM" ; |
102 | case TOKUDB_HUGE_PAGES_ENABLED: |
103 | return "TOKUDB_HUGE_PAGES_ENABLED" ; |
104 | case TOKUDB_OUT_OF_RANGE: |
105 | return "TOKUDB_OUT_OF_RANGE" ; |
106 | case TOKUDB_INTERRUPTED: |
107 | return "TOKUDB_INTERRUPTED" ; |
108 | default: |
109 | return "unknown ft error" ; |
110 | } |
111 | } |
112 | |
113 | public: |
114 | ft_exception(int c) : _code(c) {} |
115 | |
116 | int code() const noexcept { |
117 | return _code; |
118 | } |
119 | |
120 | virtual const char *what() const noexcept { |
121 | return ft_strerror(_code); |
122 | } |
123 | }; |
124 | |
125 | class system_exception : public std::exception { |
126 | int _code; |
127 | |
128 | public: |
129 | system_exception(int c) : _code(c) {} |
130 | |
131 | int code() const noexcept { |
132 | return _code; |
133 | } |
134 | |
135 | virtual const char *what() const noexcept { |
136 | return strerror(_code); |
137 | } |
138 | }; |
139 | |
140 | inline void handle_ft_retval(int r) { |
141 | if (r == 0) { |
142 | return; |
143 | } |
144 | if (r < 0) { |
145 | throw ft_exception(r); |
146 | } |
147 | if (r > 0) { |
148 | throw system_exception(r); |
149 | } |
150 | } |
151 | |
152 | } // namespace ftcxx |
153 | |