Skip to content

Commit 0026611

Browse files
committed
Add diagnostics for DTD service detection failures
- Log all registered DTD services when checking for ConnectedApp - Capture and log specific errors instead of silently swallowing exceptions - Add contextual error messages explaining possible causes - Track VM service retrieval failures separately - Make error messages dynamic based on actual failure reason
1 parent 72a9283 commit 0026611

File tree

1 file changed

+69
-23
lines changed
  • pkgs/dart_mcp_server/lib/src/mixins

1 file changed

+69
-23
lines changed

pkgs/dart_mcp_server/lib/src/mixins/dtd.dart

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ base mixin DartToolingDaemonSupport
4242
/// Once we connect to dtd, this may be toggled to `true`.
4343
bool _connectedAppServiceIsSupported = false;
4444

45+
/// Stores service detection errors for diagnostic purposes.
46+
String? _serviceDetectionError;
47+
48+
/// Stores the last error from getVmServices for diagnostic purposes.
49+
String? _vmServicesError;
50+
4551
/// Whether to await the disposal of all [VmService] objects in
4652
/// [activeVmServices] upon server shutdown or loss of DTD connection.
4753
///
@@ -68,6 +74,8 @@ base mixin DartToolingDaemonSupport
6874
_dtd = null;
6975
_activeLocation = null;
7076
_connectedAppServiceIsSupported = false;
77+
_serviceDetectionError = null;
78+
_vmServicesError = null;
7179

7280
// TODO: determine whether we need to dispose the [inspectorObjectGroup] on
7381
// the Flutter Widget Inspector for each VM service instance.
@@ -88,8 +96,23 @@ base mixin DartToolingDaemonSupport
8896
if (dtd == null) return;
8997
if (!_connectedAppServiceIsSupported) return;
9098

91-
final vmServiceInfos = (await dtd.getVmServices()).vmServicesInfos;
92-
if (vmServiceInfos.isEmpty) return;
99+
List<VmServiceInfo> vmServiceInfos;
100+
try {
101+
vmServiceInfos = (await dtd.getVmServices()).vmServicesInfos;
102+
_vmServicesError = null;
103+
log(LoggingLevel.debug,
104+
'Found ${vmServiceInfos.length} VM service(s)');
105+
} catch (e, stack) {
106+
_vmServicesError = 'Failed to get VM services: $e';
107+
log(LoggingLevel.error, _vmServicesError!);
108+
log(LoggingLevel.debug, 'Stack trace: $stack');
109+
return;
110+
}
111+
if (vmServiceInfos.isEmpty) {
112+
_vmServicesError = 'No VM services available';
113+
log(LoggingLevel.warning, _vmServicesError!);
114+
return;
115+
}
93116

94117
for (final vmServiceInfo in vmServiceInfos) {
95118
final vmServiceUri = vmServiceInfo.uri;
@@ -262,15 +285,26 @@ base mixin DartToolingDaemonSupport
262285
final dtd = _dtd!;
263286

264287
_connectedAppServiceIsSupported = false;
288+
_serviceDetectionError = null;
265289
try {
266290
final registeredServices = await dtd.getRegisteredServices();
267-
if (registeredServices.dtdServices.contains(
268-
'${ConnectedAppServiceConstants.serviceName}.'
269-
'${ConnectedAppServiceConstants.getVmServices}',
270-
)) {
291+
log(LoggingLevel.debug,
292+
'Registered DTD services: ${registeredServices.dtdServices}');
293+
final expectedService = '${ConnectedAppServiceConstants.serviceName}.'
294+
'${ConnectedAppServiceConstants.getVmServices}';
295+
if (registeredServices.dtdServices.contains(expectedService)) {
271296
_connectedAppServiceIsSupported = true;
297+
log(LoggingLevel.debug, 'ConnectedApp service detected successfully');
298+
} else {
299+
_serviceDetectionError =
300+
'Service "$expectedService" not found in registered services';
301+
log(LoggingLevel.warning, _serviceDetectionError!);
272302
}
273-
} catch (_) {}
303+
} catch (e, stack) {
304+
_serviceDetectionError = 'Failed to get registered services: $e';
305+
log(LoggingLevel.error, _serviceDetectionError!);
306+
log(LoggingLevel.debug, 'Stack trace: $stack');
307+
}
274308

275309
if (_connectedAppServiceIsSupported) {
276310
await _listenForConnectedAppServiceEvents();
@@ -997,16 +1031,21 @@ base mixin DartToolingDaemonSupport
9971031
inputSchema: Schema.object(),
9981032
);
9991033

1000-
static final _connectedAppsNotSupported = CallToolResult(
1001-
isError: true,
1002-
content: [
1003-
TextContent(
1004-
text:
1005-
'A Dart SDK of version 3.9.0-163.0.dev or greater is required to '
1006-
'connect to Dart and Flutter applications.',
1007-
),
1008-
],
1009-
)..failureReason = CallToolFailureReason.connectedAppServiceNotSupported;
1034+
CallToolResult get _connectedAppsNotSupported {
1035+
String errorText;
1036+
if (_serviceDetectionError != null) {
1037+
errorText = 'ConnectedApp service not available: $_serviceDetectionError';
1038+
} else {
1039+
errorText =
1040+
'A Dart SDK of version 3.9.0-163.0.dev or greater is required to '
1041+
'connect to Dart and Flutter applications.';
1042+
}
1043+
1044+
return CallToolResult(
1045+
isError: true,
1046+
content: [TextContent(text: errorText)],
1047+
)..failureReason = CallToolFailureReason.connectedAppServiceNotSupported;
1048+
}
10101049

10111050
static final _dtdNotConnected = CallToolResult(
10121051
isError: true,
@@ -1030,12 +1069,19 @@ base mixin DartToolingDaemonSupport
10301069
],
10311070
)..failureReason = CallToolFailureReason.dtdAlreadyConnected;
10321071

1033-
static final _noActiveDebugSession = CallToolResult(
1034-
content: [
1035-
TextContent(text: 'No active debug session to take a screenshot'),
1036-
],
1037-
isError: true,
1038-
)..failureReason = CallToolFailureReason.noActiveDebugSession;
1072+
CallToolResult get _noActiveDebugSession {
1073+
var errorText = 'No VM services available';
1074+
if (_vmServicesError != null) {
1075+
errorText += ': $_vmServicesError';
1076+
} else if (_serviceDetectionError != null) {
1077+
errorText += ' (service detection failed: $_serviceDetectionError)';
1078+
}
1079+
1080+
return CallToolResult(
1081+
content: [TextContent(text: errorText)],
1082+
isError: true,
1083+
)..failureReason = CallToolFailureReason.noActiveDebugSession;
1084+
}
10391085

10401086
static final _flutterDriverNotRegistered = CallToolResult(
10411087
content: [

0 commit comments

Comments
 (0)