Skip to content

Commit 8416bd7

Browse files
authored
Merge pull request #81 from zauguin/errorcodes
Expose error codes in exceptions
2 parents 1d7747f + a22241e commit 8416bd7

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ Errors
284284
----
285285
286286
On error, the library throws an error class indicating the type of error. The error classes are derived from the SQLITE3 error names, so if the error code is SQLITE_CONSTRAINT, the error class thrown is sqlite::exceptions::constraint. Note that all errors are derived from sqlite::sqlite_exception and that itself is derived from std::runtime_exception.
287+
sqlite::sqlite_exception has a get_code() member function to get the SQLITE3 error code.
287288
288289
```c++
289290
database db(":memory:");
@@ -297,7 +298,7 @@ On error, the library throws an error class indicating the type of error. The er
297298
/* if you are trying to catch all sqlite related exceptions
298299
* make sure to catch them by reference */
299300
catch (sqlite_exception& e) {
300-
cerr << e.what() << endl;
301+
cerr << e.get_code() << ": " << e.what() << endl;
301302
}
302303
/* you can catch specific exceptions as well,
303304
catch(sqlite::exceptions::constraint e) { } */

hdr/sqlite_modern_cpp.h

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828

2929
namespace sqlite {
3030

31-
struct sqlite_exception: public std::runtime_error {
32-
sqlite_exception(const char* msg):runtime_error(msg) {}
31+
class sqlite_exception: public std::runtime_error {
32+
public:
33+
sqlite_exception(const char* msg, int code = -1): runtime_error(msg), code(code) {}
34+
sqlite_exception(int code): runtime_error(sqlite3_errstr(code)), code(code) {}
35+
int get_code() {return code;}
36+
private:
37+
int code;
3338
};
3439

3540
namespace exceptions {
@@ -70,33 +75,33 @@ namespace sqlite {
7075
class no_rows: public sqlite_exception { using sqlite_exception::sqlite_exception; };
7176

7277
static void throw_sqlite_error(const int& error_code) {
73-
if(error_code == SQLITE_ERROR) throw exceptions::error(sqlite3_errstr(error_code));
74-
else if(error_code == SQLITE_INTERNAL) throw exceptions::internal (sqlite3_errstr(error_code));
75-
else if(error_code == SQLITE_PERM) throw exceptions::perm(sqlite3_errstr(error_code));
76-
else if(error_code == SQLITE_ABORT) throw exceptions::abort(sqlite3_errstr(error_code));
77-
else if(error_code == SQLITE_BUSY) throw exceptions::busy(sqlite3_errstr(error_code));
78-
else if(error_code == SQLITE_LOCKED) throw exceptions::locked(sqlite3_errstr(error_code));
79-
else if(error_code == SQLITE_NOMEM) throw exceptions::nomem(sqlite3_errstr(error_code));
80-
else if(error_code == SQLITE_READONLY) throw exceptions::readonly(sqlite3_errstr(error_code));
81-
else if(error_code == SQLITE_INTERRUPT) throw exceptions::interrupt(sqlite3_errstr(error_code));
82-
else if(error_code == SQLITE_IOERR) throw exceptions::ioerr(sqlite3_errstr(error_code));
83-
else if(error_code == SQLITE_CORRUPT) throw exceptions::corrupt(sqlite3_errstr(error_code));
84-
else if(error_code == SQLITE_NOTFOUND) throw exceptions::notfound(sqlite3_errstr(error_code));
85-
else if(error_code == SQLITE_FULL) throw exceptions::full(sqlite3_errstr(error_code));
86-
else if(error_code == SQLITE_CANTOPEN) throw exceptions::cantopen(sqlite3_errstr(error_code));
87-
else if(error_code == SQLITE_PROTOCOL) throw exceptions::protocol(sqlite3_errstr(error_code));
88-
else if(error_code == SQLITE_EMPTY) throw exceptions::empty(sqlite3_errstr(error_code));
89-
else if(error_code == SQLITE_SCHEMA) throw exceptions::schema(sqlite3_errstr(error_code));
90-
else if(error_code == SQLITE_TOOBIG) throw exceptions::toobig(sqlite3_errstr(error_code));
91-
else if(error_code == SQLITE_CONSTRAINT) throw exceptions::constraint(sqlite3_errstr(error_code));
92-
else if(error_code == SQLITE_MISMATCH) throw exceptions::mismatch(sqlite3_errstr(error_code));
93-
else if(error_code == SQLITE_MISUSE) throw exceptions::misuse(sqlite3_errstr(error_code));
94-
else if(error_code == SQLITE_NOLFS) throw exceptions::nolfs(sqlite3_errstr(error_code));
95-
else if(error_code == SQLITE_AUTH) throw exceptions::auth(sqlite3_errstr(error_code));
96-
else if(error_code == SQLITE_FORMAT) throw exceptions::format(sqlite3_errstr(error_code));
97-
else if(error_code == SQLITE_RANGE) throw exceptions::range(sqlite3_errstr(error_code));
98-
else if(error_code == SQLITE_NOTADB) throw exceptions::notadb(sqlite3_errstr(error_code));
99-
else throw sqlite_exception(sqlite3_errstr(error_code));
78+
if(error_code == SQLITE_ERROR) throw exceptions::error(error_code);
79+
else if(error_code == SQLITE_INTERNAL) throw exceptions::internal (error_code);
80+
else if(error_code == SQLITE_PERM) throw exceptions::perm(error_code);
81+
else if(error_code == SQLITE_ABORT) throw exceptions::abort(error_code);
82+
else if(error_code == SQLITE_BUSY) throw exceptions::busy(error_code);
83+
else if(error_code == SQLITE_LOCKED) throw exceptions::locked(error_code);
84+
else if(error_code == SQLITE_NOMEM) throw exceptions::nomem(error_code);
85+
else if(error_code == SQLITE_READONLY) throw exceptions::readonly(error_code);
86+
else if(error_code == SQLITE_INTERRUPT) throw exceptions::interrupt(error_code);
87+
else if(error_code == SQLITE_IOERR) throw exceptions::ioerr(error_code);
88+
else if(error_code == SQLITE_CORRUPT) throw exceptions::corrupt(error_code);
89+
else if(error_code == SQLITE_NOTFOUND) throw exceptions::notfound(error_code);
90+
else if(error_code == SQLITE_FULL) throw exceptions::full(error_code);
91+
else if(error_code == SQLITE_CANTOPEN) throw exceptions::cantopen(error_code);
92+
else if(error_code == SQLITE_PROTOCOL) throw exceptions::protocol(error_code);
93+
else if(error_code == SQLITE_EMPTY) throw exceptions::empty(error_code);
94+
else if(error_code == SQLITE_SCHEMA) throw exceptions::schema(error_code);
95+
else if(error_code == SQLITE_TOOBIG) throw exceptions::toobig(error_code);
96+
else if(error_code == SQLITE_CONSTRAINT) throw exceptions::constraint(error_code);
97+
else if(error_code == SQLITE_MISMATCH) throw exceptions::mismatch(error_code);
98+
else if(error_code == SQLITE_MISUSE) throw exceptions::misuse(error_code);
99+
else if(error_code == SQLITE_NOLFS) throw exceptions::nolfs(error_code);
100+
else if(error_code == SQLITE_AUTH) throw exceptions::auth(error_code);
101+
else if(error_code == SQLITE_FORMAT) throw exceptions::format(error_code);
102+
else if(error_code == SQLITE_RANGE) throw exceptions::range(error_code);
103+
else if(error_code == SQLITE_NOTADB) throw exceptions::notadb(error_code);
104+
else throw sqlite_exception(error_code);
100105
}
101106
}
102107

@@ -183,11 +188,11 @@ namespace sqlite {
183188
if((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
184189
call_back();
185190
} else if(hresult == SQLITE_DONE) {
186-
throw exceptions::no_rows("no rows to extract: exactly 1 row expected");
191+
throw exceptions::no_rows("no rows to extract: exactly 1 row expected", SQLITE_DONE);
187192
}
188193

189194
if((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
190-
throw exceptions::more_rows("not all rows extracted");
195+
throw exceptions::more_rows("not all rows extracted", SQLITE_ROW);
191196
}
192197

193198
if(hresult != SQLITE_DONE) {

tests/exceptions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main() {
1717
// inserting again to produce error
1818
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
1919
} catch (sqlite_exception& e) {
20-
cerr << e.what() << endl;
20+
cerr << e.get_code() << ": " << e.what() << endl;
2121
expception_thrown = true;
2222
} catch (...) {
2323
cerr << "Ok, we have our excpetion thrown" << endl;

0 commit comments

Comments
 (0)