Skip to content

Commit 1a6161f

Browse files
authored
Merge pull request #28 from nubank/error-handling
Error handling for DatabaseException
2 parents 1f111b5 + 1844d2d commit 1a6161f

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.4.2
2+
* Handling DatabaseException and Bad State errors
3+
14
## 1.4.1
25
* Allow Dart 3
36

lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Constants {
22
static const packageName = 'amplitude-flutter';
3-
static const packageVersion = '1.3.5';
3+
static const packageVersion = '1.4.2';
44

55
// Local storage
66
static const kLocalStoreDeviceIdKey = 'amp:device_id';

lib/src/store.dart

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,46 @@ class Store {
3131
if (db == null) {
3232
return 0;
3333
}
34-
final result = await db.insert(EVENTS_TABLE, _serialize(event));
35-
length++;
36-
return result;
34+
35+
try {
36+
final result = await db.insert(EVENTS_TABLE, _serialize(event));
37+
length++;
38+
return result;
39+
} catch (ex) {
40+
return 0;
41+
}
3742
}
3843

3944
Future<List<Object?>> addAll(List<Event> events) async {
4045
final db = await _getDb();
4146
if (db == null) {
4247
return [];
4348
}
44-
final batch = db.batch();
45-
for (final event in events) {
46-
batch.insert(EVENTS_TABLE, _serialize(event));
47-
length++;
49+
50+
try {
51+
final batch = db.batch();
52+
for (final event in events) {
53+
batch.insert(EVENTS_TABLE, _serialize(event));
54+
length++;
55+
}
56+
return await batch.commit(noResult: true);
57+
} catch (ex) {
58+
return [];
4859
}
49-
return await batch.commit(noResult: true);
5060
}
5161

5262
Future<void> empty() async {
5363
final db = await _getDb();
5464
if (db == null) {
5565
return;
5666
}
57-
await db.rawDelete('DELETE FROM $EVENTS_TABLE; VACUUM;');
58-
length = 0;
67+
68+
try {
69+
await db.rawDelete('DELETE FROM $EVENTS_TABLE; VACUUM;');
70+
length = 0;
71+
} catch (ex) {
72+
return;
73+
}
5974
}
6075

6176
Future<int?> count() async {
@@ -68,28 +83,44 @@ class Store {
6883
if (db == null) {
6984
return;
7085
}
71-
final resultCount = await db.rawDelete(
72-
'DELETE FROM $EVENTS_TABLE WHERE $COL_ID IN (SELECT T2.$COL_ID FROM $EVENTS_TABLE T2 ORDER BY T2.$COL_ID LIMIT $count)');
73-
length -= resultCount;
86+
87+
try {
88+
final resultCount = await db.rawDelete(
89+
'DELETE FROM $EVENTS_TABLE WHERE $COL_ID IN (SELECT T2.$COL_ID FROM $EVENTS_TABLE T2 ORDER BY T2.$COL_ID LIMIT $count)');
90+
length -= resultCount;
91+
} catch (ex) {
92+
return;
93+
}
7494
}
7595

7696
Future<void> delete(List<int?> eventIds) async {
7797
final db = await _getDb();
7898
if (db == null) {
7999
return;
80100
}
81-
final count = await db.rawDelete(
82-
'DELETE FROM $EVENTS_TABLE WHERE id IN (${eventIds.join(',')})');
83-
length -= count;
101+
102+
try {
103+
final count = await db.rawDelete(
104+
'DELETE FROM $EVENTS_TABLE WHERE id IN (${eventIds.join(',')})');
105+
length -= count;
106+
} catch (ex) {
107+
return;
108+
}
84109
}
85110

86111
Future<List<Event>> fetch(int count) async {
87112
final db = await _getDb();
88113
if (db == null) {
89114
return [];
90115
}
91-
final records = await db.query(EVENTS_TABLE, limit: count, orderBy: COL_ID);
92-
return records.map((m) => _deserialize(m)).toList();
116+
117+
try {
118+
final records =
119+
await db.query(EVENTS_TABLE, limit: count, orderBy: COL_ID);
120+
return records.map((m) => _deserialize(m)).toList();
121+
} catch (ex) {
122+
return [];
123+
}
93124
}
94125

95126
Future<Database?> _init() async {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: amplitude_flutter
22
description: Official Flutter plugin for tracking events and revenue to Amplitude.
3-
version: 1.4.0
3+
version: 1.4.2
44
homepage: https://github.com/amplitude/Amplitude-Flutter
55

66
environment:

0 commit comments

Comments
 (0)