Skip to content

Commit 4321aec

Browse files
authored
[unified_analytics] Suppress any FileSystemException thrown during Analytics.send (#274)
1 parent e5d4c8b commit 4321aec

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.1.1
2+
3+
- Fixed bug where calling `Analytics.send` could result in a `FileSystemException` when unable to write to a log file.
4+
15
## 6.1.0
26

37
- Added new event constructor `Event.devtoolsEvent` for the single devtools event with a new enum value `DashEvent.devtoolsEvent`

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
8282
const String kLogFileName = 'dart-flutter-telemetry.log';
8383

8484
/// The current version of the package, should be in line with pubspec version.
85-
const String kPackageVersion = '6.1.0';
85+
const String kPackageVersion = '6.1.1';
8686

8787
/// The minimum length for a session.
8888
const int kSessionDurationMinutes = 30;

pkgs/unified_analytics/lib/src/log_handler.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,20 @@ class LogHandler {
275275
var records = logFile.readAsLinesSync();
276276
final content = '${jsonEncode(data)}\n';
277277

278-
// When the record count is less than the max, add as normal;
279-
// else drop the oldest records until equal to max
280-
if (records.length < kLogFileLength) {
281-
logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
282-
} else {
283-
records.add(content);
284-
records = records.skip(records.length - kLogFileLength).toList();
285-
286-
logFile.writeAsStringSync(records.join('\n'));
278+
try {
279+
// When the record count is less than the max, add as normal;
280+
// else drop the oldest records until equal to max
281+
if (records.length < kLogFileLength) {
282+
logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
283+
} else {
284+
records.add(content);
285+
records = records.skip(records.length - kLogFileLength).toList();
286+
287+
logFile.writeAsStringSync(records.join('\n'));
288+
}
289+
} on FileSystemException {
290+
// Logging isn't important enough to warrant raising a
291+
// FileSystemException that will surprise consumers of this package.
287292
}
288293
}
289294
}

pkgs/unified_analytics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: >-
44
to Google Analytics.
55
# When updating this, keep the version consistent with the changelog and the
66
# value in lib/src/constants.dart.
7-
version: 6.1.0
7+
version: 6.1.1
88
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
99

1010
environment:

pkgs/unified_analytics/test/log_handler_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:test/test.dart';
99

1010
import 'package:unified_analytics/src/constants.dart';
1111
import 'package:unified_analytics/src/enums.dart';
12+
import 'package:unified_analytics/src/log_handler.dart';
1213
import 'package:unified_analytics/src/utils.dart';
1314
import 'package:unified_analytics/unified_analytics.dart';
1415

@@ -191,6 +192,26 @@ void main() {
191192
expect(logFile.readAsLinesSync()[0].trim(), isNot('{{'));
192193
});
193194

195+
test(
196+
'Catches and discards any FileSystemException raised from attempting '
197+
'to write to the log file', () async {
198+
final logFilePath = 'log.txt';
199+
final fs = MemoryFileSystem.test(opHandle: (context, operation) {
200+
if (context == logFilePath && operation == FileSystemOp.write) {
201+
throw FileSystemException(
202+
'writeFrom failed',
203+
logFilePath,
204+
const OSError('No space left on device', 28),
205+
);
206+
}
207+
});
208+
final logFile = fs.file(logFilePath);
209+
logFile.createSync();
210+
final logHandler = LogHandler(logFile: logFile);
211+
212+
logHandler.save(data: {});
213+
});
214+
194215
test('Catching cast errors for each log record silently', () async {
195216
// Write a json array to the log file which will cause
196217
// a cast error when parsing each line

0 commit comments

Comments
 (0)