Enhance error logging for SqliteException

* Enhance error logging for SqliteException to include the return code in logging output to make it easier to understand why the sqlite error has occurred.
This commit is contained in:
abraunegg 2024-03-25 17:57:17 +11:00
parent 35ebdc87d4
commit c592ba5a25
2 changed files with 13 additions and 9 deletions

View file

@ -207,12 +207,13 @@ final class ItemDatabase {
// An error was generated - what was the error? // An error was generated - what was the error?
if (e.msg == "database is locked") { if (e.msg == "database is locked") {
addLogEntry(); addLogEntry();
addLogEntry("ERROR: onedrive application is already running - check system process list for active application instances"); addLogEntry("ERROR: The 'onedrive' application is already running - please check system process list for active application instances");
addLogEntry(" - Use 'sudo ps aufxw | grep onedrive' to potentially determine acive running process", ["verbose"]); addLogEntry(" - Use 'sudo ps aufxw | grep onedrive' to potentially determine acive running process", ["verbose"]);
addLogEntry(); addLogEntry();
} else { } else {
// A different error .. detail the message, detail the actual SQLite Error Code to assist with troubleshooting
addLogEntry(); addLogEntry();
addLogEntry("ERROR: An internal database error occurred: " ~ e.msg); addLogEntry("ERROR: An internal database error occurred: " ~ e.msg ~ " (SQLite Error Code: " ~ to!string(e.errorCode) ~ ")");
addLogEntry(); addLogEntry();
} }
return; return;

View file

@ -15,7 +15,7 @@ extern (C) immutable(char)* sqlite3_errstr(int); // missing from the std library
static this() { static this() {
if (sqlite3_libversion_number() < 3006019) { if (sqlite3_libversion_number() < 3006019) {
throw new SqliteException("sqlite 3.6.19 or newer is required"); throw new SqliteException(-1,"sqlite 3.6.19 or newer is required");
} }
} }
@ -24,14 +24,17 @@ private string ifromStringz(const(char)* cstr) {
} }
class SqliteException: Exception { class SqliteException: Exception {
@safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) int errorCode; // Add an errorCode member to store the SQLite error code
@safe pure nothrow this(int errorCode, string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
{ {
super(msg, file, line, next); super(msg, file, line, next);
this.errorCode = errorCode; // Set the errorCode
} }
@safe pure nothrow this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @safe pure nothrow this(int errorCode, string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
{ {
super(msg, file, line, next); super(msg, file, line, next);
this.errorCode = errorCode; // Set the errorCode
} }
} }
@ -104,7 +107,7 @@ struct Database {
} }
int rc = sqlite3_exec(pDb, "PRAGMA user_version", &callback, &userVersion, null); int rc = sqlite3_exec(pDb, "PRAGMA user_version", &callback, &userVersion, null);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
throw new SqliteException(ifromStringz(sqlite3_errmsg(pDb))); throw new SqliteException(rc, ifromStringz(sqlite3_errmsg(pDb)));
} }
return userVersion; return userVersion;
} }
@ -129,7 +132,7 @@ struct Database {
// https://www.sqlite.org/c3ref/prepare.html // https://www.sqlite.org/c3ref/prepare.html
int rc = sqlite3_prepare_v2(pDb, zSql.ptr, cast(int) zSql.length, &s.pStmt, null); int rc = sqlite3_prepare_v2(pDb, zSql.ptr, cast(int) zSql.length, &s.pStmt, null);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
throw new SqliteException(ifromStringz(sqlite3_errmsg(pDb))); throw new SqliteException(rc, ifromStringz(sqlite3_errmsg(pDb)));
} }
return s; return s;
} }
@ -204,7 +207,7 @@ struct Statement {
// https://www.sqlite.org/c3ref/bind_blob.html // https://www.sqlite.org/c3ref/bind_blob.html
int rc = sqlite3_bind_text(pStmt, index, value.ptr, cast(int) value.length, SQLITE_STATIC); int rc = sqlite3_bind_text(pStmt, index, value.ptr, cast(int) value.length, SQLITE_STATIC);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
throw new SqliteException(ifromStringz(sqlite3_errmsg(sqlite3_db_handle(pStmt)))); throw new SqliteException(rc, ifromStringz(sqlite3_errmsg(sqlite3_db_handle(pStmt))));
} }
} }
@ -217,7 +220,7 @@ struct Statement {
// https://www.sqlite.org/c3ref/reset.html // https://www.sqlite.org/c3ref/reset.html
int rc = sqlite3_reset(pStmt); int rc = sqlite3_reset(pStmt);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
throw new SqliteException(ifromStringz(sqlite3_errmsg(sqlite3_db_handle(pStmt)))); throw new SqliteException(rc, ifromStringz(sqlite3_errmsg(sqlite3_db_handle(pStmt))));
} }
} }
} }