Catch StateError and output more useful error message when DDS fails to start (#72736)

This commit is contained in:
Ben Konyi
2020-12-22 10:24:04 -08:00
committed by GitHub
parent af1c629def
commit 620a8284f8
3 changed files with 65 additions and 11 deletions

View File

@@ -11,6 +11,14 @@ import 'common.dart';
import 'io.dart' as io;
import 'logger.dart';
@visibleForTesting
Future<dds.DartDevelopmentService> Function(
Uri,
{bool enableAuthCodes,
bool ipv6,
Uri serviceUri,
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;
/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes.
class DartDevelopmentService {
@@ -44,7 +52,7 @@ class DartDevelopmentService {
'connecting to VM service at $observatoryUri.',
);
try {
_ddsInstance = await dds.DartDevelopmentService.startDartDevelopmentService(
_ddsInstance = await ddsLauncherCallback(
observatoryUri,
serviceUri: ddsUri,
enableAuthCodes: !disableServiceAuthCodes,
@@ -59,9 +67,21 @@ class DartDevelopmentService {
} on dds.DartDevelopmentServiceException catch (e) {
logger.printTrace('Warning: Failed to start DDS: ${e.message}');
if (e.errorCode == dds.DartDevelopmentServiceException.existingDdsInstanceError) {
_existingDdsUri = Uri.parse(
e.message.split(' ').firstWhere((String e) => e.startsWith('http'))
);
try {
_existingDdsUri = Uri.parse(
e.message.split(' ').firstWhere((String e) => e.startsWith('http'))
);
} on StateError {
logger.printError(
'DDS has failed to start and there is not an existing DDS instance '
'available to connect to. Please comment on '
'https://github.com/flutter/flutter/issues/72385 with output from '
'"flutter doctor -v" and the following error message:\n\n ${e.message}.'
);
// Wrap the DDS error message in a StateError so it can be collected
// by the crash handler.
throw StateError(e.message);
}
}
if (!_completer.isCompleted) {
_completer.complete();

View File

@@ -236,10 +236,10 @@ class FlutterDevice {
bool existingDds = false;
vm_service.VmService service;
if (!disableDds) {
void handleError(Exception e) {
void handleError(Exception e, StackTrace st) {
globals.printTrace('Fail to connect to service protocol: $observatoryUri: $e');
if (!completer.isCompleted) {
completer.completeError('failed to connect to $observatoryUri');
completer.completeError('failed to connect to $observatoryUri', st);
}
}
// This first try block is meant to catch errors that occur during DDS startup
@@ -252,16 +252,16 @@ class FlutterDevice {
ipv6,
disableServiceAuthCodes,
);
} on dds.DartDevelopmentServiceException catch (e) {
} on dds.DartDevelopmentServiceException catch (e, st) {
if (!allowExistingDdsInstance ||
(e.errorCode != dds.DartDevelopmentServiceException.existingDdsInstanceError)) {
handleError(e);
handleError(e, st);
return;
} else {
existingDds = true;
}
} on Exception catch (e) {
handleError(e);
} on Exception catch (e, st) {
handleError(e, st);
return;
}
}