diff --git a/packages/flutter_tools/lib/src/base/net.dart b/packages/flutter_tools/lib/src/base/net.dart index 24b90b6f08..343898f14c 100644 --- a/packages/flutter_tools/lib/src/base/net.dart +++ b/packages/flutter_tools/lib/src/base/net.dart @@ -48,6 +48,9 @@ Future> _attempt(Uri url) async { 'URL: $url', exitCode: kNetworkProblemExitCode, ); + } on SocketException catch (error) { + printTrace('Download error: $error'); + return null; } final HttpClientResponse response = await request.close(); if (response.statusCode != 200) { diff --git a/packages/flutter_tools/test/base/net_test.dart b/packages/flutter_tools/test/base/net_test.dart index 922c72c1d6..ddab776f00 100644 --- a/packages/flutter_tools/test/base/net_test.dart +++ b/packages/flutter_tools/test/base/net_test.dart @@ -17,8 +17,8 @@ void main() { new FakeAsync().run((FakeAsync time) { fetchUrl(Uri.parse('http://example.invalid/')).then((List value) { error = 'test completed unexpectedly'; - }, onError: (dynamic error) { - error = 'test failed unexpectedly'; + }, onError: (dynamic exception) { + error = 'test failed unexpectedly: $exception'; }); expect(testLogger.statusText, ''); time.elapse(const Duration(milliseconds: 10000)); @@ -40,8 +40,8 @@ void main() { new FakeAsync().run((FakeAsync time) { fetchUrl(Uri.parse('http://example.invalid/')).then((List value) { error = 'test completed unexpectedly'; - }, onError: (dynamic error) { - error = 'test failed unexpectedly'; + }, onError: (dynamic exception) { + error = 'test failed unexpectedly: $exception'; }); expect(testLogger.statusText, ''); time.elapse(const Duration(milliseconds: 10000)); @@ -57,6 +57,68 @@ void main() { }, overrides: { HttpClientFactory: () => () => new MockHttpClient(200), }); + + testUsingContext('retry from SocketException', () async { + String error; + new FakeAsync().run((FakeAsync time) { + fetchUrl(Uri.parse('http://example.invalid/')).then((List value) { + error = 'test completed unexpectedly'; + }, onError: (dynamic exception) { + error = 'test failed unexpectedly: $exception'; + }); + expect(testLogger.statusText, ''); + time.elapse(const Duration(milliseconds: 10000)); + expect(testLogger.statusText, + 'Download failed -- attempting retry 1 in 1 second...\n' + 'Download failed -- attempting retry 2 in 2 seconds...\n' + 'Download failed -- attempting retry 3 in 4 seconds...\n' + 'Download failed -- attempting retry 4 in 8 seconds...\n' + ); + }); + expect(testLogger.errorText, isEmpty); + expect(error, isNull); + expect(testLogger.traceText, contains('Download error: SocketException')); + }, overrides: { + HttpClientFactory: () => () => new MockHttpClientThrowing( + const io.SocketException('test exception handling'), + ), + }); + + testUsingContext('no retry from HandshakeException', () async { + String error; + new FakeAsync().run((FakeAsync time) { + fetchUrl(Uri.parse('http://example.invalid/')).then((List value) { + error = 'test completed unexpectedly'; + }, onError: (dynamic exception) { + error = 'test failed: $exception'; + }); + expect(testLogger.statusText, ''); + time.elapse(const Duration(milliseconds: 10000)); + expect(testLogger.statusText, ''); + }); + expect(error, startsWith('test failed')); + expect(testLogger.traceText, contains('HandshakeException')); + }, overrides: { + HttpClientFactory: () => () => new MockHttpClientThrowing( + const io.HandshakeException('test exception handling'), + ), + }); +} + +class MockHttpClientThrowing implements io.HttpClient { + MockHttpClientThrowing(this.exception); + + final Exception exception; + + @override + Future getUrl(Uri url) async { + throw exception; + } + + @override + dynamic noSuchMethod(Invocation invocation) { + throw 'io.HttpClient - $invocation'; + } } class MockHttpClient implements io.HttpClient {