diff --git a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart index e4dea3a1f9..af65c4593f 100644 --- a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart +++ b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart @@ -223,11 +223,29 @@ class FlutterDebugAdapter extends DartDebugAdapter? errorData = data?.data; + if (errorData == null) { + return; + } + + final String errorText = (errorData['renderedErrorText'] as String?) + ?? (errorData['description'] as String?) + // We should never not error text, but if we do at least send something + // so it's not just completely silent. + ?? 'Unknown error in Flutter.Error event'; + sendOutput('stderr', '$errorText\n'); + } + /// Called by [launchRequest] to request that we actually start the app to be run/debugged. /// /// For debugging, this should start paused, connect to the VM Service, set @@ -240,6 +258,12 @@ class FlutterDebugAdapter extends DartDebugAdapter testExceptionOutput({required bool noDebug}) async { + final BasicProjectThatThrows project = BasicProjectThatThrows(); + await project.setUpIn(tempDir); + + final List outputEvents = + await dap.client.collectAllOutput(launch: () { + // Terminate the app after we see the exception because otherwise + // it will keep running and `collectAllOutput` won't end. + dap.client.output + .firstWhere((String output) => output.contains(endOfErrorOutputMarker)) + .then((_) => dap.client.terminate()); + return dap.client.launch( + noDebug: noDebug, + cwd: project.dir.path, + toolArgs: ['-d', 'flutter-tester'], + ); + }); + + final String output = _uniqueOutputLines(outputEvents); + final List outputLines = output.split('\n'); + expect( outputLines, containsAllInOrder([ + '══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════', + 'The following _Exception was thrown building App(dirty):', + 'Exception: c', + 'The relevant error-causing widget was:', + ])); + expect(output, contains('App:${Uri.file(project.dir.path)}/lib/main.dart:24:12')); + } + + testWithoutContext('correctly outputs exceptions in debug mode', () => testExceptionOutput(noDebug: false)); + + testWithoutContext('correctly outputs exceptions in noDebug mode', () => testExceptionOutput(noDebug: true)); + testWithoutContext('can hot reload', () async { final BasicProject project = BasicProject(); await project.setUpIn(tempDir); diff --git a/packages/flutter_tools/test/integration.shard/debug_adapter/test_support.dart b/packages/flutter_tools/test/integration.shard/debug_adapter/test_support.dart index 2d747e768a..563c564a34 100644 --- a/packages/flutter_tools/test/integration.shard/debug_adapter/test_support.dart +++ b/packages/flutter_tools/test/integration.shard/debug_adapter/test_support.dart @@ -33,6 +33,9 @@ final bool useInProcessDap = Platform.environment['DAP_TEST_INTERNAL'] == 'true' /// Service traffic (wrapped in a custom 'dart.log' event). final bool verboseLogging = Platform.environment['DAP_TEST_VERBOSE'] == 'true'; +const String startOfErrorOutputMarker = '══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════'; +const String endOfErrorOutputMarker = '════════════════════════════════════════════════════════════════════════════════════════════════════'; + /// Expects the lines in [actual] to match the relevant matcher in [expected], /// ignoring differences in line endings and trailing whitespace. void expectLines(