[flutter_tools] Hanlde OSError in places where we've seen it thrown (#52491)

This commit is contained in:
Zachary Anderson 2020-03-12 14:01:01 -07:00 committed by GitHub
parent 183da8f837
commit 0cd2ece539
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 62 deletions

View file

@ -78,6 +78,7 @@ export 'dart:io'
IOSink,
// Link NO! Use `file_system.dart`
// NetworkInterface NO! Use `io.dart`
OSError,
pid,
// Platform NO! use `platform.dart`
Process,

View file

@ -319,7 +319,12 @@ class _DevFSHttpWriter {
onError: (dynamic error) { globals.printTrace('error: $error'); },
cancelOnError: true);
break;
} on Exception catch (error, trace) {
} catch (error, trace) { // ignore: avoid_catches_without_on_clauses
// We treat OSError as an Exception.
// See: https://github.com/dart-lang/sdk/issues/40934
if (error is! Exception && error is! OSError) {
rethrow;
}
if (!_completer.isCompleted) {
globals.printTrace('Error writing "$deviceUri" to DevFS: $error');
if (retry > 0) {

View file

@ -133,6 +133,11 @@ class MDnsObservatoryDiscovery {
authCode += '/';
}
return MDnsObservatoryDiscoveryResult(srv.first.port, authCode);
} on OSError catch (e) {
// OSError is neither an Error nor and Exception, so we wrap it in a
// SocketException and rethrow.
// See: https://github.com/dart-lang/sdk/issues/40934
throw SocketException('mdns query failed', osError: e);
} finally {
client.stop();
}

View file

@ -109,7 +109,13 @@ void main() {
HttpOverrides.global = savedHttpOverrides;
});
testUsingContext('retry uploads when failure', () async {
final List<dynamic> exceptions = <dynamic>[
Exception('Connection resert by peer'),
const OSError('Connection reset by peer'),
];
for (final dynamic exception in exceptions) {
testUsingContext('retry uploads when failure: $exception', () async {
final File file = fs.file(fs.path.join(basePath, filePath));
await file.parent.create(recursive: true);
file.writeAsBytesSync(<int>[1, 2, 3]);
@ -136,7 +142,7 @@ void main() {
const int kFailedAttempts = 5;
when(httpRequest.close()).thenAnswer((Invocation invocation) {
if (nRequest++ < kFailedAttempts) {
throw Exception('Connection resert by peer');
throw exception;
}
return Future<HttpClientResponse>.value(httpClientResponse);
});
@ -162,6 +168,7 @@ void main() {
HttpClientFactory: () => () => httpClient,
ProcessManager: () => FakeProcessManager.any(),
});
}
});
group('devfs remote', () {

View file

@ -192,6 +192,21 @@ void main() {
final int port = (await portDiscovery.query(applicationId: 'bar'))?.port;
expect(port, isNull);
});
testUsingContext('Throws SocketException when client throws OSError on start', () async {
final MDnsClient client = MockMDnsClient();
when(client.start()).thenAnswer((_) {
throw const OSError('Operation not suppoted on socket', 102);
});
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client,
);
expect(
() async => await portDiscovery.query(),
throwsA(isA<SocketException>()),
);
});
});
}