diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index 3cebcf2d..b28088a0 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.1.1 + +- Fixed bug where calling `Analytics.send` could result in a `FileSystemException` when unable to write to a log file. + ## 6.1.0 - Added new event constructor `Event.devtoolsEvent` for the single devtools event with a new enum value `DashEvent.devtoolsEvent` diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index eb848349..9ac9d1ac 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -82,7 +82,7 @@ const int kLogFileLength = 2500; const String kLogFileName = 'dart-flutter-telemetry.log'; /// The current version of the package, should be in line with pubspec version. -const String kPackageVersion = '6.1.0'; +const String kPackageVersion = '6.1.1'; /// The minimum length for a session. const int kSessionDurationMinutes = 30; diff --git a/pkgs/unified_analytics/lib/src/log_handler.dart b/pkgs/unified_analytics/lib/src/log_handler.dart index 6a0f0585..d611b73a 100644 --- a/pkgs/unified_analytics/lib/src/log_handler.dart +++ b/pkgs/unified_analytics/lib/src/log_handler.dart @@ -275,15 +275,20 @@ class LogHandler { var records = logFile.readAsLinesSync(); final content = '${jsonEncode(data)}\n'; - // When the record count is less than the max, add as normal; - // else drop the oldest records until equal to max - if (records.length < kLogFileLength) { - logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend); - } else { - records.add(content); - records = records.skip(records.length - kLogFileLength).toList(); - - logFile.writeAsStringSync(records.join('\n')); + try { + // When the record count is less than the max, add as normal; + // else drop the oldest records until equal to max + if (records.length < kLogFileLength) { + logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend); + } else { + records.add(content); + records = records.skip(records.length - kLogFileLength).toList(); + + logFile.writeAsStringSync(records.join('\n')); + } + } on FileSystemException { + // Logging isn't important enough to warrant raising a + // FileSystemException that will surprise consumers of this package. } } } diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index ed96d605..11c778b3 100644 --- a/pkgs/unified_analytics/pubspec.yaml +++ b/pkgs/unified_analytics/pubspec.yaml @@ -4,7 +4,7 @@ description: >- to Google Analytics. # When updating this, keep the version consistent with the changelog and the # value in lib/src/constants.dart. -version: 6.1.0 +version: 6.1.1 repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: diff --git a/pkgs/unified_analytics/test/log_handler_test.dart b/pkgs/unified_analytics/test/log_handler_test.dart index 76ae5e64..3cacf472 100644 --- a/pkgs/unified_analytics/test/log_handler_test.dart +++ b/pkgs/unified_analytics/test/log_handler_test.dart @@ -9,6 +9,7 @@ import 'package:test/test.dart'; import 'package:unified_analytics/src/constants.dart'; import 'package:unified_analytics/src/enums.dart'; +import 'package:unified_analytics/src/log_handler.dart'; import 'package:unified_analytics/src/utils.dart'; import 'package:unified_analytics/unified_analytics.dart'; @@ -191,6 +192,26 @@ void main() { expect(logFile.readAsLinesSync()[0].trim(), isNot('{{')); }); + test( + 'Catches and discards any FileSystemException raised from attempting ' + 'to write to the log file', () async { + final logFilePath = 'log.txt'; + final fs = MemoryFileSystem.test(opHandle: (context, operation) { + if (context == logFilePath && operation == FileSystemOp.write) { + throw FileSystemException( + 'writeFrom failed', + logFilePath, + const OSError('No space left on device', 28), + ); + } + }); + final logFile = fs.file(logFilePath); + logFile.createSync(); + final logHandler = LogHandler(logFile: logFile); + + logHandler.save(data: {}); + }); + test('Catching cast errors for each log record silently', () async { // Write a json array to the log file which will cause // a cast error when parsing each line