diff --git a/packages/flutter_tools/lib/src/reporting/crash_reporting.dart b/packages/flutter_tools/lib/src/reporting/crash_reporting.dart index 9f8205c610..e16de4a385 100644 --- a/packages/flutter_tools/lib/src/reporting/crash_reporting.dart +++ b/packages/flutter_tools/lib/src/reporting/crash_reporting.dart @@ -127,7 +127,7 @@ class CrashReportSender { printError('Failed to send crash report. Server responded with HTTP status code ${resp.statusCode}'); } } catch (sendError, sendStackTrace) { - if (sendError is SocketException) { + if (sendError is SocketException || sendError is HttpException) { printError('Failed to send crash report due to a network error: $sendError'); } else { // If the sender itself crashes, just print. We did our best. diff --git a/packages/flutter_tools/test/general.shard/crash_reporting_test.dart b/packages/flutter_tools/test/general.shard/crash_reporting_test.dart index f905c240d6..cd908864b7 100644 --- a/packages/flutter_tools/test/general.shard/crash_reporting_test.dart +++ b/packages/flutter_tools/test/general.shard/crash_reporting_test.dart @@ -59,6 +59,48 @@ void main() { Stdio: () => const _NoStderr(), }); + testUsingContext('should print an explanatory message when there is a SocketException', () async { + final Completer exitCodeCompleter = Completer(); + setExitFunctionForTests((int exitCode) { + exitCodeCompleter.complete(exitCode); + }); + + CrashReportSender.initializeWith( + CrashingCrashReportSender(const SocketException('no internets'))); + + unawaited(tools.run( + ['crash'], + [_CrashAsyncCommand()], + reportCrashes: true, + flutterVersion: 'test-version', + )); + expect(await exitCodeCompleter.future, 1); + expect(testLogger.errorText, contains('Failed to send crash report due to a network error')); + }, overrides: { + Stdio: () => const _NoStderr(), + }); + + testUsingContext('should print an explanatory message when there is an HttpException', () async { + final Completer exitCodeCompleter = Completer(); + setExitFunctionForTests((int exitCode) { + exitCodeCompleter.complete(exitCode); + }); + + CrashReportSender.initializeWith( + CrashingCrashReportSender(const HttpException('no internets'))); + + unawaited(tools.run( + ['crash'], + [_CrashAsyncCommand()], + reportCrashes: true, + flutterVersion: 'test-version', + )); + expect(await exitCodeCompleter.future, 1); + expect(testLogger.errorText, contains('Failed to send crash report due to a network error')); + }, overrides: { + Stdio: () => const _NoStderr(), + }); + testUsingContext('should send crash reports when async throws', () async { final Completer exitCodeCompleter = Completer(); setExitFunctionForTests((int exitCode) { @@ -270,6 +312,13 @@ class MockCrashReportSender extends MockClient { static int sendCalls = 0; } +class CrashingCrashReportSender extends MockClient { + CrashingCrashReportSender(Object exception) + : super((Request request) async { + throw exception; + }); +} + /// Throws a random error to simulate a CLI crash. class _CrashCommand extends FlutterCommand {