Skip to content

Commit fdcac45

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Print extended SQLite error code." into jb-dev
2 parents 4d6a0df + ca309f2 commit fdcac45

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

core/jni/android_database_SQLiteCommon.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "android_database_SQLiteCommon.h"
1818

19+
#include <utils/String8.h>
20+
1921
namespace android {
2022

2123
/* throw a SQLiteException with a message appropriate for the error in handle */
@@ -37,7 +39,8 @@ void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message)
3739
// the error message may contain more information than the error code
3840
// because it is based on the extended error code rather than the simplified
3941
// error code that SQLite normally returns.
40-
throw_sqlite3_exception(env, sqlite3_errcode(handle), sqlite3_errmsg(handle), message);
42+
throw_sqlite3_exception(env, sqlite3_extended_errcode(handle),
43+
sqlite3_errmsg(handle), message);
4144
} else {
4245
// we use SQLITE_OK so that a generic SQLiteException is thrown;
4346
// any code not specified in the switch statement below would do.
@@ -49,9 +52,7 @@ void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message)
4952
* should only be used when the database connection is not available because the
5053
* error information will not be quite as rich */
5154
void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode, const char* message) {
52-
char temp[21];
53-
sprintf(temp, "error code %d", errcode);
54-
throw_sqlite3_exception(env, errcode, temp, message);
55+
throw_sqlite3_exception(env, errcode, "unknown error", message);
5556
}
5657

5758
/* throw a SQLiteException for a given error code, sqlite3message, and
@@ -60,7 +61,7 @@ void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode, const char* messa
6061
void throw_sqlite3_exception(JNIEnv* env, int errcode,
6162
const char* sqlite3Message, const char* message) {
6263
const char* exceptionClass;
63-
switch (errcode) {
64+
switch (errcode & 0xff) { /* mask off extended error code */
6465
case SQLITE_IOERR:
6566
exceptionClass = "android/database/sqlite/SQLiteDiskIOException";
6667
break;
@@ -119,19 +120,15 @@ void throw_sqlite3_exception(JNIEnv* env, int errcode,
119120
break;
120121
}
121122

122-
if (sqlite3Message != NULL && message != NULL) {
123-
char* fullMessage = (char *)malloc(strlen(sqlite3Message) + strlen(message) + 3);
124-
if (fullMessage != NULL) {
125-
strcpy(fullMessage, sqlite3Message);
126-
strcat(fullMessage, ": ");
127-
strcat(fullMessage, message);
128-
jniThrowException(env, exceptionClass, fullMessage);
129-
free(fullMessage);
130-
} else {
131-
jniThrowException(env, exceptionClass, sqlite3Message);
123+
if (sqlite3Message) {
124+
String8 fullMessage;
125+
fullMessage.append(sqlite3Message);
126+
fullMessage.appendFormat(" (code %d)", errcode); // print extended error code
127+
if (message) {
128+
fullMessage.append(": ");
129+
fullMessage.append(message);
132130
}
133-
} else if (sqlite3Message != NULL) {
134-
jniThrowException(env, exceptionClass, sqlite3Message);
131+
jniThrowException(env, exceptionClass, fullMessage.string());
135132
} else {
136133
jniThrowException(env, exceptionClass, message);
137134
}

0 commit comments

Comments
 (0)