Skip to content

Commit

Permalink
Use custom ReloadReport
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
srujzs committed Jan 30, 2025
1 parent 410cdc3 commit 9617510
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions dwds/lib/src/services/chrome_proxy_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, dynamic> toJson() {
final jsonified = <String, Object?>{
'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 ?? <String>[]) {
jsonified[jsonKey] = super.json![jsonKey];
}
return jsonified;
}
}

/// The `type`s of [ConsoleAPIEvent]s that are treated as `stderr` logs.
const _stderrTypes = ['error'];

Expand Down

0 comments on commit 9617510

Please sign in to comment.