From 9617510a3dd8d3125a9105c697edaa67a64c92d3 Mon Sep 17 00:00:00 2001 From: Srujan Gaddam Date: Thu, 30 Jan 2025 09:12:50 -0800 Subject: [PATCH] Use custom ReloadReport The ReloadReport gets jsonified using `toJson` so we need a custom class to override it so that `toJson` includes all the things we need to transmit errors. --- .../src/services/chrome_proxy_service.dart | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/dwds/lib/src/services/chrome_proxy_service.dart b/dwds/lib/src/services/chrome_proxy_service.dart index 88dd4298c..1b231045d 100644 --- a/dwds/lib/src/services/chrome_proxy_service.dart +++ b/dwds/lib/src/services/chrome_proxy_service.dart @@ -1162,13 +1162,17 @@ class ChromeProxyService implements VmServiceInterface { ); _logger.info('\$dartHotReloadDwds request complete.'); } catch (e) { - // TODO(srujzs): We should bubble up the error, but `ReloadReport` needs - // more fields to capture that info. _logger.info('Hot reload failed: $e'); - return ReloadReport(success: false); + final report = _ReloadReportWithMetadata(success: false); + report.json = { + 'notices': [ + {'message': e.toString()}, + ], + }; + return report; } _logger.info('Successful hot reload'); - return ReloadReport(success: true); + return _ReloadReportWithMetadata(success: true); } @override @@ -1757,6 +1761,28 @@ class ChromeProxyService implements VmServiceInterface { } } +// The default `ReloadReport`'s `toJson` only emits the type and success of the +// operation. Override it to expose additional possible metadata in the `json`. +// This then gets called in the VM service code that handles request and +// responses. +class _ReloadReportWithMetadata extends ReloadReport { + _ReloadReportWithMetadata({super.success}); + + @override + Map toJson() { + final jsonified = { + 'type': type, + 'success': success ?? false, + }; + // Include any other metadata we may have included in `json`, potentially + // even overriding the above defaults. + for (final jsonKey in super.json?.keys ?? []) { + jsonified[jsonKey] = super.json![jsonKey]; + } + return jsonified; + } +} + /// The `type`s of [ConsoleAPIEvent]s that are treated as `stderr` logs. const _stderrTypes = ['error'];