Skip to content

Commit 20415d2

Browse files
authored
Forward errors through sendRequest for Peers (#2225)
Avoid a future that does not complete. Forward errors through both the peer and client forwarders.
1 parent 31b4885 commit 20415d2

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

pkgs/json_rpc_2/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.0-wip
2+
3+
* Forward errors within `Peer` when it is acting as a client.
4+
15
## 4.0.0
26

37
* Add custom ID generator option to clients, which allows for `String` ids.

pkgs/json_rpc_2/dart_test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
platforms:
2+
- vm
3+
- chrome
4+
5+
compilers:
6+
- dart2js
7+
- dart2wasm

pkgs/json_rpc_2/lib/src/peer.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Peer implements Client, Server {
161161
}
162162
}, onError: (Object error, StackTrace stackTrace) {
163163
_serverIncomingForwarder.addError(error, stackTrace);
164+
_clientIncomingForwarder.addError(error, stackTrace);
164165
}, onDone: close);
165166
return done;
166167
}

pkgs/json_rpc_2/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_rpc_2
2-
version: 4.0.0
2+
version: 4.1.0-wip
33
description: >-
44
Utilities to write a client or server using the JSON-RPC 2.0 spec.
55
repository: https://github.com/dart-lang/tools/tree/main/pkgs/json_rpc_2

pkgs/json_rpc_2/test/peer_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ void main() {
4545
peer.sendRequest('foo', {'bar': 'baz'}), completion(equals('qux')));
4646
});
4747

48+
test('can send a message and receive an error', () {
49+
expect(outgoing.first.then((request) {
50+
expect(
51+
request,
52+
equals({
53+
'jsonrpc': '2.0',
54+
'method': 'foo',
55+
'params': {'bar': 'baz'},
56+
'id': 0
57+
}));
58+
incoming.addError(Exception('failure'));
59+
}), completes);
60+
61+
peer.listen().ignore();
62+
63+
expect(
64+
peer.sendRequest('foo', {'bar': 'baz'}),
65+
throwsA(isA<StateError>().having(($) => $.message, 'message',
66+
contains('client closed with pending request "foo"'))));
67+
});
68+
4869
test('can send a batch of messages and receive a batch of responses', () {
4970
expect(outgoing.first.then((request) {
5071
expect(

0 commit comments

Comments
 (0)