The _routeRequest method in vm_service.dart has a bug where it catches and re-encodes RPCError exceptions as kServerError, losing the original error code and information.
This PR applies the following changes to the _routeRequest method:
- Preserves RPCError exceptions: When an RPCError is caught, it returns the error as-is using e.toMap(), maintaining the original error code and information.
- Wraps other exceptions: Only non-RPCError exceptions are wrapped as kServerError, which is the appropriate behavior for unexpected errors.
This ensures that when service callbacks throw specific RPCError instances (like kIsolateCannotReload), those error codes are properly propagated to the client instead of being incorrectly replaced with kServerError.
fixes: https://github.com/dart-lang/sdk/issues/61757
Change-Id: Ib67c78035215372d1cc2c73d8c819e2825aad5bb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/458040
Auto-Submit: Jessy Yameogo <yjessy@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
diff --git a/pkg/vm_service/CHANGELOG.md b/pkg/vm_service/CHANGELOG.md
index d5bb0fd..a77a155 100644
--- a/pkg/vm_service/CHANGELOG.md
+++ b/pkg/vm_service/CHANGELOG.md
@@ -1,6 +1,8 @@
## 15.1.0
- Update to version `4.20` of the spec.
- Deprecate `streamCpuSamplesWithUserTag` RPC.
+- Fix bug where `RPCError` exceptions thrown by service callbacks were incorrectly
+ wrapped as `kServerError`, losing the original error code.
## 15.0.2
- Add `kTimerSignificantlyOverdue` field to `EventKind`.
diff --git a/pkg/vm_service/lib/src/vm_service.dart b/pkg/vm_service/lib/src/vm_service.dart
index 1705bbc..10677dc 100644
--- a/pkg/vm_service/lib/src/vm_service.dart
+++ b/pkg/vm_service/lib/src/vm_service.dart
@@ -2095,7 +2095,11 @@
try {
return await service(params);
+ } on RPCError catch (e) {
+ // Preserve RPCError exceptions as-is.
+ return {'error': e.toMap()};
} catch (e, st) {
+ // Wrap other exceptions.
final error = RPCError.withDetails(
method,
RPCErrorKind.kServerError.code,