Skip to content

Commit 5eb4de4

Browse files
committed
fix(SQLite): handle SQLite errors.
1 parent 0ee970a commit 5eb4de4

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

AndroidSDK/src/com/leanplum/internal/LeanplumEventDataManager.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class LeanplumEventDataManager {
5858
private static LeanplumDataBaseManager databaseManager;
5959
private static ContentValues contentValues = new ContentValues();
6060

61+
static boolean willSendErrorLog = false;
62+
6163
/**
6264
* Creates connection to database, if database is not present, it will automatically create it.
6365
*
@@ -76,8 +78,7 @@ public static void init(Context context) {
7678
}
7779
database = databaseManager.getWritableDatabase();
7880
} catch (Throwable t) {
79-
Log.e("Cannot create database.", t);
80-
Util.handleException(t);
81+
handleSQLiteError("Cannot create database.", t);
8182
}
8283
}
8384

@@ -93,9 +94,9 @@ static void insertEvent(String event) {
9394
contentValues.put(COLUMN_DATA, event);
9495
try {
9596
database.insert(EVENT_TABLE_NAME, null, contentValues);
97+
willSendErrorLog = false;
9698
} catch (Throwable t) {
97-
Log.e("Unable to insert event to database.", t);
98-
Util.handleException(t);
99+
handleSQLiteError("Unable to insert event to database.", t);
99100
}
100101
contentValues.clear();
101102
}
@@ -115,14 +116,14 @@ static List<Map<String, Object>> getEvents(int count) {
115116
try {
116117
cursor = database.query(EVENT_TABLE_NAME, new String[] {COLUMN_DATA}, null, null, null,
117118
null, KEY_ROWID + " ASC", "" + count);
119+
willSendErrorLog = false;
118120
while (cursor.moveToNext()) {
119121
Map<String, Object> requestArgs = JsonConverter.mapFromJson(new JSONObject(
120122
cursor.getString(cursor.getColumnIndex(COLUMN_DATA))));
121123
events.add(requestArgs);
122124
}
123125
} catch (Throwable t) {
124-
Log.e("Unable to get events from the table.", t);
125-
Util.handleException(t);
126+
handleSQLiteError("Unable to get events from the table.", t);
126127
} finally {
127128
if (cursor != null) {
128129
cursor.close();
@@ -143,9 +144,9 @@ static void deleteEvents(int count) {
143144
try {
144145
database.delete(EVENT_TABLE_NAME, KEY_ROWID + " in (select " + KEY_ROWID + " from " +
145146
EVENT_TABLE_NAME + " ORDER BY " + KEY_ROWID + " ASC LIMIT " + count + ")", null);
147+
willSendErrorLog = false;
146148
} catch (Throwable t) {
147-
Log.e("Unable to delete events from the table.", t);
148-
Util.handleException(t);
149+
handleSQLiteError("Unable to delete events from the table.", t);
149150
}
150151
}
151152

@@ -161,13 +162,25 @@ static long getEventsCount() {
161162
}
162163
try {
163164
count = DatabaseUtils.queryNumEntries(database, EVENT_TABLE_NAME);
165+
willSendErrorLog = false;
164166
} catch (Throwable t) {
165-
Log.e("Unable to get a number of rows in the table.", t);
166-
Util.handleException(t);
167+
handleSQLiteError("Unable to get a number of rows in the table.", t);
167168
}
168169
return count;
169170
}
170171

172+
/**
173+
* Helper function that logs and sends errors to the server.
174+
*/
175+
private static void handleSQLiteError(String log, Throwable t) {
176+
Log.e(log, t);
177+
// Send error log. Using willSendErrorLog to prevent infinte loop.
178+
if (!willSendErrorLog) {
179+
willSendErrorLog = true;
180+
Util.handleException(t);
181+
}
182+
}
183+
171184
private static class LeanplumDataBaseManager extends SQLiteOpenHelper {
172185
LeanplumDataBaseManager(Context context) {
173186
super(context, DATABASE_NAME, null, DATABASE_VERSION);

AndroidSDK/src/com/leanplum/internal/Request.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class Request {
8787

8888
private static ApiResponseCallback apiResponse;
8989

90+
private static List<Map<String, Object>> localErrors = new ArrayList<>();
91+
9092
public static void setAppId(String appId, String accessKey) {
9193
Request.appId = appId;
9294
Request.accessKey = accessKey;
@@ -144,7 +146,10 @@ public Request(String httpMethod, String apiMethod, Map<String, Object> params)
144146
this.httpMethod = httpMethod;
145147
this.apiMethod = apiMethod;
146148
this.params = params != null ? params : new HashMap<String, Object>();
147-
149+
// Check if it is error and here was SQLite exception.
150+
if (Constants.Methods.LOG.equals(apiMethod) && LeanplumEventDataManager.willSendErrorLog) {
151+
localErrors.add(createArgsDictionary());
152+
}
148153
// Make sure the Handler is initialized on the main thread.
149154
OsHandler.getInstance();
150155
}
@@ -387,9 +392,21 @@ protected Void doInBackground(Void... params) {
387392
}
388393

389394
private void sendRequests() {
390-
List<Map<String, Object>> unsentRequests = getUnsentRequests();
391-
List<Map<String, Object>> requestsToSend =
392-
removeIrrelevantBackgroundStartRequests(unsentRequests);
395+
List<Map<String, Object>> unsentRequests = new ArrayList<>();
396+
List<Map<String, Object>> requestsToSend;
397+
// Check if we have localErrors, if yes then we will send only errors to the server.
398+
if (localErrors.size() != 0) {
399+
String uuid = UUID.randomUUID().toString();
400+
for (Map<String, Object> error : localErrors) {
401+
error.put(UUID_KEY, uuid);
402+
unsentRequests.add(error);
403+
}
404+
requestsToSend = unsentRequests;
405+
} else {
406+
unsentRequests = getUnsentRequests();
407+
requestsToSend = removeIrrelevantBackgroundStartRequests(unsentRequests);
408+
}
409+
393410
if (requestsToSend.isEmpty()) {
394411
return;
395412
}
@@ -420,6 +437,8 @@ private void sendRequests() {
420437

421438
Exception errorException = null;
422439
if (statusCode >= 200 && statusCode <= 299) {
440+
// Clear localErrrors list.
441+
localErrors.clear();
423442
deleteSentRequests(unsentRequests.size());
424443
if (unsentRequests.size() == MAX_EVENTS_PER_API_CALL) {
425444
sendRequests();

0 commit comments

Comments
 (0)