Unverified Commit 85be28d3 authored by jcollins-g's avatar jcollins-g Committed by GitHub

Add catch for SocketException and tests for exception handling on fetchUrl. (#16569)

parent c103fd0c
......@@ -48,6 +48,9 @@ Future<List<int>> _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) {
......
......@@ -17,8 +17,8 @@ void main() {
new FakeAsync().run((FakeAsync time) {
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> 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<int> 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: <Type, Generator>{
HttpClientFactory: () => () => new MockHttpClient(200),
});
testUsingContext('retry from SocketException', () async {
String error;
new FakeAsync().run((FakeAsync time) {
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> 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: <Type, Generator>{
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<int> 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: <Type, Generator>{
HttpClientFactory: () => () => new MockHttpClientThrowing(
const io.HandshakeException('test exception handling'),
),
});
}
class MockHttpClientThrowing implements io.HttpClient {
MockHttpClientThrowing(this.exception);
final Exception exception;
@override
Future<io.HttpClientRequest> getUrl(Uri url) async {
throw exception;
}
@override
dynamic noSuchMethod(Invocation invocation) {
throw 'io.HttpClient - $invocation';
}
}
class MockHttpClient implements io.HttpClient {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment