[dtd] Improve the error thrown when trying to call a service method without a complete name

This now behaves the same as for any other method that doesn't exist, rather than throwing a generic RPC error with a `RangeError` inside.

Fixes https://github.com/dart-lang/sdk/issues/56540

Change-Id: I1171fe1c0c2a4a6f56d7a653af87b17a76af9d68
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381643
Reviewed-by: Elliott Brooks <elliottbrooks@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
This commit is contained in:
Danny Tuppeny
2024-08-22 18:03:50 +00:00
committed by Commit Queue
parent 531e4ca0ec
commit 44cc865f7d
2 changed files with 23 additions and 0 deletions
+8
View File
@@ -311,6 +311,14 @@ class DTDClient extends Client {
// method, forward the request to that client.
final combinedName = parameters.method;
final dotIndex = combinedName.indexOf('.');
if (dotIndex == -1) {
// All service methods must have a dot in the name.
throw json_rpc.RpcException(
RpcErrorCodes.kMethodNotFound,
'Unknown service method: $combinedName',
);
}
final serviceName = combinedName.substring(0, dotIndex);
final methodName = combinedName.substring(dotIndex + 1);
+15
View File
@@ -330,6 +330,21 @@ void main() {
);
});
test('calling a method without a dot', () {
expect(
() => client.sendRequest('abc', {}),
throwsA(
isA<RpcException>()
.having(
(e) => e.code,
'code',
RpcException.methodNotFound('abc').code,
)
.having((e) => e.message, 'message', 'Unknown method "abc".'),
),
);
});
test('different clients cannot register the same service', () async {
final client2 = _createClient(uri);
final registerResult = await client.sendRequest('registerService', {