Skip to content

WebSocket.addError needs documentation #45733

Open
@ltOgt

Description

Related to #30920 and #31504.

When working with simple streams:

import 'dart:async';

main() {
  StreamController c = StreamController();
  c.stream.listen(
    (event) {
      print("[<<<]$event[>>>]");
    },
    onError: (error) {
      print("[<<<]$error[>>>]");
    },
    onDone: () => print("done"),
    cancelOnError: false,
  );
  c.add("OK");
  c.addError("error");
}

Handles both add and addError as expected.

When working with websockets:
(client.dart)

import 'dart:io';

main() async {
  late WebSocket socket;
  socket = await WebSocket.connect(
    'ws://localhost:4040/test',
  );

  socket.listen(
    (event) {
      print("[<<<]$event[>>>]");
    },
    onError: (error) {
      print("[<<<]$error[>>>]");
    },
    onDone: () => print("done"),
    cancelOnError: false,
  );
}

(server.dart)

import 'dart:io';

main() async {
  HttpServer server = await HttpServer.bind('127.0.0.1', 4040);

  await for (HttpRequest req in server) {
    if (req.uri.path == '/test') {
      // Upgrade a HttpRequest to a WebSocket connection.
      WebSocket socket = await WebSocketTransformer.upgrade(req);

      socket.add("OK");
      socket.addError("error");
    }
  }
}

server.dart will throw

Unhandled exception:
error

As I understood it, the exception is thrown because no listener for onError exists, since only the client listens for the data.

Looking back it makes sense that the socket can only send one stream of data, but the API was somewhat confusing.
It led me to believe that I can simply approach the problem like I am used to with simple streams.

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-iotype-documentationA request to add or improve documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions