Skip to content

Commit

Permalink
ignore corrupt events on disk (users might edit it on web)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Apr 3, 2024
1 parent 9b5e006 commit 3f16b5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/src/analytics/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:wiredash/src/core/version.dart';
import 'package:wiredash/src/core/wiredash_widget.dart';

// Required
// TODO ignore corrupt events on disk (users might edit it on web)
// TODO export analytics
// TODO Write documentation

Expand Down
6 changes: 3 additions & 3 deletions lib/src/analytics/event_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PersistentAnalyticsEventStore implements AnalyticsEventStore {
await prefs.reload();
final keys = prefs.getKeys();

final Map<String, AnalyticsEvent> toBeSubmitted = {};
final Map<String, AnalyticsEvent> result = {};
for (final key in keys) {
final match = eventKeyRegex.firstMatch(key);
if (match == null) continue;
Expand All @@ -75,7 +75,7 @@ class PersistentAnalyticsEventStore implements AnalyticsEventStore {
try {
final AnalyticsEvent event =
deserializeEventV1(jsonDecode(eventJson) as Map<String, Object?>);
toBeSubmitted[key] = event;
result[key] = event;
} catch (e, stack) {
reportWiredashInfo(
e,
Expand All @@ -86,7 +86,7 @@ class PersistentAnalyticsEventStore implements AnalyticsEventStore {
}
}
}
return toBeSubmitted;
return result;
}

@override
Expand Down
36 changes: 36 additions & 0 deletions test/analytics/event_store_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wiredash/src/analytics/event_store.dart';

import '../util/flutter_error.dart';

void main() {
test('ignore invalid events on disk', () async {
SharedPreferences.setMockInitialValues({});
final errors = captureFlutterErrors();
final eventStore = PersistentAnalyticsEventStore(
sharedPreferences: SharedPreferences.getInstance,
);

final prefs = await SharedPreferences.getInstance();
await prefs.setString(
'io.wiredash.events.default|1234567890|abc', '{"invalid": "event"}');
final events = await eventStore.getEvents(null);

errors.restoreDefaultErrorHandlers();

expect(events, hasLength(0));
// reported warning
expect(
errors.presentErrorText,
contains(
'Error when parsing event io.wiredash.events.default|1234567890|abc. Removing',
),
);
expect(
prefs.getString('io.wiredash.events.default|1234567890|abc'),
isNull,
reason: 'Invalid event should be removed from disk',
);
});
}

0 comments on commit 3f16b5d

Please sign in to comment.