Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9d25fa67 authored by Jeff Brown's avatar Jeff Brown
Browse files

Report extended error information from SQLite.

The error code number is not as informative as the SQLite error
message, in particular because the error code has been stripped
of extended error information.  Make sure we ask SQLite for the
full error message whenever possible.

Bug: 6538393
Change-Id: I82457c0ff7e41659cf8195fa26e09dc2b467375e
parent 433927c5
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -33,8 +33,11 @@ void throw_sqlite3_exception(JNIEnv* env, const char* message) {
 */
void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message) {
    if (handle) {
        throw_sqlite3_exception(env, sqlite3_errcode(handle),
                                sqlite3_errmsg(handle), message);
        // get the error code and message from the SQLite connection
        // the error message may contain more information than the error code
        // because it is based on the extended error code rather than the simplified
        // error code that SQLite normally returns.
        throw_sqlite3_exception(env, sqlite3_errcode(handle), sqlite3_errmsg(handle), message);
    } else {
        // we use SQLITE_OK so that a generic SQLiteException is thrown;
        // any code not specified in the switch statement below would do.
@@ -42,16 +45,14 @@ void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message)
    }
}

/* throw a SQLiteException for a given error code */
/* throw a SQLiteException for a given error code
 * should only be used when the database connection is not available because the
 * error information will not be quite as rich */
void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode, const char* message) {
    if (errcode == SQLITE_DONE) {
        throw_sqlite3_exception(env, errcode, NULL, message);
    } else {
    char temp[21];
    sprintf(temp, "error code %d", errcode);
    throw_sqlite3_exception(env, errcode, temp, message);
}
}

/* throw a SQLiteException for a given error code, sqlite3message, and
   user message
@@ -75,6 +76,7 @@ void throw_sqlite3_exception(JNIEnv* env, int errcode,
            break;
        case SQLITE_DONE:
            exceptionClass = "android/database/sqlite/SQLiteDoneException";
            sqlite3Message = NULL; // SQLite error message is irrelevant in this case
            break;
        case SQLITE_FULL:
            exceptionClass = "android/database/sqlite/SQLiteFullException";
+2 −2
Original line number Diff line number Diff line
@@ -444,7 +444,7 @@ static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_st
        throw_sqlite3_exception(env,
                "Queries can be performed using SQLiteDatabase query or rawQuery methods only.");
    } else if (err != SQLITE_DONE) {
        throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(connection->db));
        throw_sqlite3_exception(env, connection->db);
    }
    return err;
}
@@ -479,7 +479,7 @@ static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz,
static int executeOneRowQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) {
    int err = sqlite3_step(statement);
    if (err != SQLITE_ROW) {
        throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(connection->db));
        throw_sqlite3_exception(env, connection->db);
    }
    return err;
}