Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
Do not use `sqlite3_errmsg` when `sqlite3_errcode` doesn't match
result code.
Use `sqlite3_errstr` instead.
  • Loading branch information
gwenn committed Dec 5, 2024
1 parent ab4ae7c commit a2b9cfb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/sqlite/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class SQLite implements Library {
// Applications can use the sqlite3_log(E,F,..) API to send new messages to the log, if desired, but this is discouraged.
public static native void sqlite3_log(int iErrCode, String msg);

static native String sqlite3_errstr(int rc);
static native String sqlite3_errmsg(SQLite3 pDb); // copy needed: the error string might be overwritten or deallocated by subsequent calls to other SQLite interface functions.
static native int sqlite3_errcode(SQLite3 pDb);

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/sqlite/SQLiteException.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public SQLiteException(Conn c, String reason, int errCode, Throwable cause) {
super(reason, null, errCode, cause);
if (c == null) {
errMsg = null;
} else if (getErrorCode() >= 0) {
errMsg = c.getErrMsg();
} else if (errCode >= 0) {
if (c.getErrCode() == errCode) {
errMsg = c.getErrMsg();
} else {
errMsg = SQLite.sqlite3_errstr(errCode);
}
} else {
errMsg = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sqlite/driver/Guard.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void closeAll(AutoCloseable... acs) throws SQLException {

void close() throws SQLException;

class SneakyGuard<T> implements AutoCloseable, Runnable {
class SneakyGuard implements AutoCloseable, Runnable {
private final Guard guard;
public SneakyGuard(Guard guard) {
this.guard = guard;
Expand Down

0 comments on commit a2b9cfb

Please sign in to comment.