[flutter_tool] Observatory connection error handling cleanup (#38353)

This commit is contained in:
Zachary Anderson
2019-08-15 12:13:03 -07:00
committed by GitHub
parent f5dcbdab3a
commit a40ab895cf
6 changed files with 266 additions and 95 deletions

View File

@@ -243,6 +243,8 @@ class AttachCommand extends FlutterCommand {
// Determine ipv6 status from the scanned logs.
usesIpv6 = observatoryDiscovery.ipv6;
printStatus('Done.'); // FYI, this message is used as a sentinel in tests.
} catch (error) {
throwToolExit('Failed to establish a debug connection with ${device.name}: $error');
} finally {
await observatoryDiscovery?.cancel();
}

View File

@@ -152,9 +152,9 @@ class IOSDevice extends Device {
@override
final String name;
Map<ApplicationPackage, _IOSDeviceLogReader> _logReaders;
Map<ApplicationPackage, DeviceLogReader> _logReaders;
_IOSDevicePortForwarder _portForwarder;
DevicePortForwarder _portForwarder;
@override
Future<bool> get isLocalEmulator async => false;
@@ -342,65 +342,55 @@ class IOSDevice extends Device {
if (platformArgs['trace-startup'] ?? false)
launchArguments.add('--trace-startup');
int installationResult = -1;
Uri localObservatoryUri;
final Status installStatus = logger.startProgress(
'Installing and launching...',
timeout: timeoutConfiguration.slowOperation);
try {
ProtocolDiscovery observatoryDiscovery;
if (debuggingOptions.debuggingEnabled) {
// Debugging is enabled, look for the observatory server port post launch.
printTrace('Debugging is enabled, connecting to observatory');
final Status installStatus = logger.startProgress('Installing and launching...', timeout: timeoutConfiguration.slowOperation);
// TODO(danrubel): The Android device class does something similar to this code below.
// The various Device subclasses should be refactored and common code moved into the superclass.
observatoryDiscovery = ProtocolDiscovery.observatory(
getLogReader(app: package),
portForwarder: portForwarder,
hostPort: debuggingOptions.observatoryPort,
ipv6: ipv6,
);
}
if (!debuggingOptions.debuggingEnabled) {
// If debugging is not enabled, just launch the application and continue.
printTrace('Debugging is not enabled');
installationResult = await const IOSDeploy().runApp(
final int installationResult = await const IOSDeploy().runApp(
deviceId: id,
bundlePath: bundle.path,
launchArguments: launchArguments,
);
} else {
// Debugging is enabled, look for the observatory server port post launch.
printTrace('Debugging is enabled, connecting to observatory');
if (installationResult != 0) {
printError('Could not install ${bundle.path} on $id.');
printError('Try launching Xcode and selecting "Product > Run" to fix the problem:');
printError(' open ios/Runner.xcworkspace');
printError('');
return LaunchResult.failed();
}
// TODO(danrubel): The Android device class does something similar to this code below.
// The various Device subclasses should be refactored and common code moved into the superclass.
final ProtocolDiscovery observatoryDiscovery = ProtocolDiscovery.observatory(
getLogReader(app: package),
portForwarder: portForwarder,
hostPort: debuggingOptions.observatoryPort,
ipv6: ipv6,
);
final Future<Uri> forwardObservatoryUri = observatoryDiscovery.uri;
final Future<int> launch = const IOSDeploy().runApp(
deviceId: id,
bundlePath: bundle.path,
launchArguments: launchArguments,
);
localObservatoryUri = await launch.then<Uri>((int result) async {
installationResult = result;
if (result != 0) {
printTrace('Failed to launch the application on device.');
return null;
}
if (!debuggingOptions.debuggingEnabled) {
return LaunchResult.succeeded();
}
try {
printTrace('Application launched on the device. Waiting for observatory port.');
return await forwardObservatoryUri;
}).whenComplete(() {
observatoryDiscovery.cancel();
});
final Uri localUri = await observatoryDiscovery.uri;
return LaunchResult.succeeded(observatoryUri: localUri);
} catch (error) {
printError('Failed to establish a debug connection with $id: $error');
return LaunchResult.failed();
} finally {
await observatoryDiscovery?.cancel();
}
} finally {
installStatus.stop();
}
installStatus.stop();
if (installationResult != 0) {
printError('Could not install ${bundle.path} on $id.');
printError('Try launching Xcode and selecting "Product > Run" to fix the problem:');
printError(' open ios/Runner.xcworkspace');
printError('');
return LaunchResult.failed();
}
return LaunchResult.succeeded(observatoryUri: localObservatoryUri);
}
@override
@@ -417,13 +407,24 @@ class IOSDevice extends Device {
@override
DeviceLogReader getLogReader({ ApplicationPackage app }) {
_logReaders ??= <ApplicationPackage, _IOSDeviceLogReader>{};
_logReaders ??= <ApplicationPackage, DeviceLogReader>{};
return _logReaders.putIfAbsent(app, () => _IOSDeviceLogReader(this, app));
}
@visibleForTesting
void setLogReader(ApplicationPackage app, DeviceLogReader logReader) {
_logReaders ??= <ApplicationPackage, DeviceLogReader>{};
_logReaders[app] = logReader;
}
@override
DevicePortForwarder get portForwarder => _portForwarder ??= _IOSDevicePortForwarder(this);
@visibleForTesting
set portForwarder(DevicePortForwarder forwarder) {
_portForwarder = forwarder;
}
@override
void clearLogs() { }

View File

@@ -65,9 +65,9 @@ class ProtocolDiscovery {
if (match != null) {
try {
uri = Uri.parse(match[1]);
} catch (error) {
} catch (error, stackTrace) {
_stopScrapingLogs();
_completer.completeError(error);
_completer.completeError(error, stackTrace);
}
}