Skip to content

Commit ab5e2da

Browse files
committed
[unified_analytics] Suppress any FileSystemException thrown during Analytics.send (#274)
1 parent 1afd581 commit ab5e2da

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
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+
## 5.8.8+2
2+
3+
- Fixed bug where calling `Analytics.send` could result in a `FileSystemException` when unable to write to a log file.
4+
15
## 5.8.8+1
26

37
- Edit to error handler to not use default `Analytic.send` method and use new `Analytics._sendError` method that doesn't create a session id

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const String kConfigString = '''
2626
# All other lines are configuration lines. They have
2727
# the form "name=value". If multiple lines contain
2828
# the same configuration name with different values,
29-
# the parser will default to a conservative value.
29+
# the parser will default to a conservative value.
3030
3131
# DISABLING TELEMETRY REPORTING
3232
#
@@ -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 = '5.8.8+1';
85+
const String kPackageVersion = '5.8.8+2';
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
@@ -285,15 +285,20 @@ class LogHandler {
285285
var records = logFile.readAsLinesSync();
286286
final content = '${jsonEncode(data)}\n';
287287

288-
// When the record count is less than the max, add as normal;
289-
// else drop the oldest records until equal to max
290-
if (records.length < kLogFileLength) {
291-
logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
292-
} else {
293-
records.add(content);
294-
records = records.skip(records.length - kLogFileLength).toList();
295-
296-
logFile.writeAsStringSync(records.join('\n'));
288+
try {
289+
// When the record count is less than the max, add as normal;
290+
// else drop the oldest records until equal to max
291+
if (records.length < kLogFileLength) {
292+
logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
293+
} else {
294+
records.add(content);
295+
records = records.skip(records.length - kLogFileLength).toList();
296+
297+
logFile.writeAsStringSync(records.join('\n'));
298+
}
299+
} on FileSystemException {
300+
// Logging isn't important enough to warrant raising a
301+
// FileSystemException that will surprise consumers of this package.
297302
}
298303
}
299304
}

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: 5.8.8+1
7+
version: 5.8.8+2
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

@@ -200,6 +201,26 @@ void main() {
200201
// expect(logFile.readAsLinesSync()[0].trim(), isNot('{{'));
201202
});
202203

204+
test(
205+
'Catches and discards any FileSystemException raised from attempting '
206+
'to write to the log file', () async {
207+
final logFilePath = 'log.txt';
208+
final fs = MemoryFileSystem.test(opHandle: (context, operation) {
209+
if (context == logFilePath && operation == FileSystemOp.write) {
210+
throw FileSystemException(
211+
'writeFrom failed',
212+
logFilePath,
213+
const OSError('No space left on device', 28),
214+
);
215+
}
216+
});
217+
final logFile = fs.file(logFilePath);
218+
logFile.createSync();
219+
final logHandler = LogHandler(logFile: logFile);
220+
221+
logHandler.save(data: {});
222+
});
223+
203224
test('Catching cast errors for each log record silently', () async {
204225
// Write a json array to the log file which will cause
205226
// a cast error when parsing each line

0 commit comments

Comments
 (0)