Skip to content

Commit 16441c6

Browse files
Victowolfdart-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
Clarify WebSocket.addError behavior
Document that WebSocket.addError does not transmit an error event to the remote peer and only reports the error locally. Changes: * sdk/lib/_http/websocket.dart Bug: #45733 Change-Id: I8913ac4fb466e9d613b3f0c5f0727640c9d9a913 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/502500 Reviewed-by: Brian Quinlan <bquinlan@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Brian Quinlan <bquinlan@google.com>
1 parent 02b30d5 commit 16441c6

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

sdk/lib/_http/websocket.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ abstract class WebSocket
433433
/// be either a `String`, or a `List<int>` holding bytes.
434434
void add(/*String|List<int>*/ data);
435435

436+
/// Reports an error on this WebSocket.
437+
///
438+
/// This does not send an error event to the remote peer.
439+
/// WebSocket connections only transmit text and binary data frames.
440+
///
441+
/// If unhandled, the error may be reported as an uncaught
442+
/// asynchronous error.
443+
void addError(Object error, [StackTrace? stackTrace]);
444+
436445
/// Sends data from a stream on WebSocket connection. Each data event from
437446
/// [stream] will be send as a single WebSocket frame. The data from [stream]
438447
/// must be either `String`s, or `List<int>`s holding bytes.

tests/standalone/io/web_socket_test.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,52 @@ class SecurityConfiguration {
252252
});
253253
}
254254

255+
void testAddErrorHandledLocally() {
256+
asyncStart();
257+
258+
bool remoteErrorReceived = false;
259+
260+
runZonedGuarded(
261+
() async {
262+
final server = await createServer();
263+
264+
late StreamSubscription clientSubscription;
265+
late WebSocket clientSocket;
266+
267+
server.listen((request) async {
268+
final serverSocket = await WebSocketTransformer.upgrade(request);
269+
270+
// Errors added with addError are reported locally through
271+
// async error handling and are not transmitted to the peer.
272+
serverSocket.addError("local error");
273+
274+
await serverSocket.close();
275+
});
276+
277+
clientSocket = await createClient(server.port);
278+
279+
clientSubscription = clientSocket.listen(
280+
(_) {},
281+
onError: (_) {
282+
remoteErrorReceived = true;
283+
},
284+
);
285+
286+
await Future.delayed(Duration(milliseconds: 100));
287+
288+
await clientSubscription.cancel();
289+
await clientSocket.close();
290+
await server.close(force: true);
291+
},
292+
(Object error, StackTrace stackTrace) async {
293+
Expect.equals("local error", error);
294+
Expect.isFalse(remoteErrorReceived);
295+
296+
asyncEnd();
297+
},
298+
);
299+
}
300+
255301
void testDoubleCloseClient() {
256302
createServer().then((server) {
257303
server.transform(new WebSocketTransformer()).listen((webSocket) {
@@ -664,6 +710,7 @@ class SecurityConfiguration {
664710
testCancelThenClose();
665711
testCloseThenCancel();
666712
testListenAfterClose();
713+
testAddErrorHandledLocally();
667714
testDoubleCloseClient();
668715
testDoubleCloseServer();
669716
testImmediateCloseServer();

0 commit comments

Comments
 (0)