[core] refactor SQLite error/status codes
This commit is contained in:
@@ -28,7 +28,7 @@ OfflineDatabase::~OfflineDatabase() {
|
||||
statements.clear();
|
||||
db.reset();
|
||||
} catch (mapbox::sqlite::Exception& ex) {
|
||||
Log::Error(Event::Database, ex.code, ex.what());
|
||||
Log::Error(Event::Database, (int)ex.code, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,13 +57,13 @@ void OfflineDatabase::ensureSchema() {
|
||||
removeExisting();
|
||||
connect(mapbox::sqlite::ReadWrite | mapbox::sqlite::Create);
|
||||
} catch (mapbox::sqlite::Exception& ex) {
|
||||
if (ex.code != mapbox::sqlite::Exception::Code::CANTOPEN && ex.code != mapbox::sqlite::Exception::Code::NOTADB) {
|
||||
if (ex.code != mapbox::sqlite::ResultCode::CantOpen && ex.code != mapbox::sqlite::ResultCode::NotADB) {
|
||||
Log::Error(Event::Database, "Unexpected error connecting to database: %s", ex.what());
|
||||
throw;
|
||||
}
|
||||
|
||||
try {
|
||||
if (ex.code == mapbox::sqlite::Exception::Code::NOTADB) {
|
||||
if (ex.code == mapbox::sqlite::ResultCode::NotADB) {
|
||||
removeExisting();
|
||||
}
|
||||
connect(mapbox::sqlite::ReadWrite | mapbox::sqlite::Create);
|
||||
|
||||
@@ -19,16 +19,49 @@ enum OpenFlag : int {
|
||||
PrivateCache = 0x00040000,
|
||||
};
|
||||
|
||||
struct Exception : std::runtime_error {
|
||||
enum Code : int {
|
||||
OK = 0,
|
||||
CANTOPEN = 14,
|
||||
NOTADB = 26
|
||||
};
|
||||
enum class ResultCode : int {
|
||||
OK = 0,
|
||||
Error = 1,
|
||||
Internal = 2,
|
||||
Perm = 3,
|
||||
Abort = 4,
|
||||
Busy = 5,
|
||||
Locked = 6,
|
||||
NoMem = 7,
|
||||
ReadOnly = 8,
|
||||
Interrupt = 9,
|
||||
IOErr = 10,
|
||||
Corrupt = 11,
|
||||
NotFound = 12,
|
||||
Full = 13,
|
||||
CantOpen = 14,
|
||||
Protocol = 15,
|
||||
Schema = 17,
|
||||
TooBig = 18,
|
||||
Constraint = 19,
|
||||
Mismatch = 20,
|
||||
Misuse = 21,
|
||||
NoLFS = 22,
|
||||
Auth = 23,
|
||||
Range = 25,
|
||||
NotADB = 26
|
||||
};
|
||||
|
||||
Exception(int err, const char *msg) : std::runtime_error(msg), code(err) {}
|
||||
Exception(int err, const std::string& msg) : std::runtime_error(msg), code(err) {}
|
||||
const int code = OK;
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
Exception(int err, const char* msg)
|
||||
: std::runtime_error(msg), code(static_cast<ResultCode>(err)) {
|
||||
}
|
||||
Exception(ResultCode err, const char* msg)
|
||||
: std::runtime_error(msg), code(err) {
|
||||
}
|
||||
Exception(int err, const std::string& msg)
|
||||
: std::runtime_error(msg), code(static_cast<ResultCode>(err)) {
|
||||
}
|
||||
Exception(ResultCode err, const std::string& msg)
|
||||
: std::runtime_error(msg), code(err) {
|
||||
}
|
||||
const ResultCode code = ResultCode::OK;
|
||||
};
|
||||
|
||||
class DatabaseImpl;
|
||||
|
||||
@@ -24,11 +24,11 @@ namespace mapbox {
|
||||
namespace sqlite {
|
||||
|
||||
// https://www.sqlite.org/rescode.html#ok
|
||||
static_assert(mbgl::underlying_type(Exception::OK) == 0, "error");
|
||||
static_assert(mbgl::underlying_type(ResultCode::OK) == 0, "error");
|
||||
// https://www.sqlite.org/rescode.html#cantopen
|
||||
static_assert(mbgl::underlying_type(Exception::CANTOPEN) == 14, "error");
|
||||
static_assert(mbgl::underlying_type(ResultCode::CantOpen) == 14, "error");
|
||||
// https://www.sqlite.org/rescode.html#notadb
|
||||
static_assert(mbgl::underlying_type(Exception::NOTADB) == 26, "error");
|
||||
static_assert(mbgl::underlying_type(ResultCode::NotADB) == 26, "error");
|
||||
|
||||
void checkQueryError(const QSqlQuery& query) {
|
||||
QSqlError lastError = query.lastError();
|
||||
@@ -57,7 +57,7 @@ void checkDatabaseOpenError(const QSqlDatabase &db) {
|
||||
// always returns -1 for `nativeErrorCode()` on database errors.
|
||||
QSqlError lastError = db.lastError();
|
||||
if (lastError.type() != QSqlError::NoError) {
|
||||
throw Exception { Exception::Code::CANTOPEN, "Error opening the database." };
|
||||
throw Exception { ResultCode::CantOpen, "Error opening the database." };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
: connectionName(QString::number(uint64_t(QThread::currentThread())) + incrementCounter())
|
||||
{
|
||||
if (!QSqlDatabase::drivers().contains("QSQLITE")) {
|
||||
throw Exception { Exception::Code::CANTOPEN, "SQLite driver not found." };
|
||||
throw Exception { ResultCode::CantOpen, "SQLite driver not found." };
|
||||
}
|
||||
|
||||
assert(!QSqlDatabase::contains(connectionName));
|
||||
|
||||
@@ -33,6 +33,6 @@ TEST(SQLite, TEST_REQUIRES_WRITE(CantOpenException)) {
|
||||
mapbox::sqlite::Database("test/fixtures/offline_database/foobar123.db", mapbox::sqlite::ReadOnly);
|
||||
FAIL();
|
||||
} catch (mapbox::sqlite::Exception& ex) {
|
||||
ASSERT_EQ(ex.code, mapbox::sqlite::Exception::Code::CANTOPEN);
|
||||
ASSERT_EQ(ex.code, mapbox::sqlite::ResultCode::CantOpen);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user