dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
423 stars 112 forks source link

App crashes even when exceptions are handled #268

Open Dnathan33 opened 1 year ago

Dnathan33 commented 1 year ago

Below is the implementation I am using for handling web socket requests.

WebSocketChannel.connect(Uri.parse(endpoint))

      final messageController = StreamController(onCancel: () {
        channel.sink.close();
      });

      channel.stream.listen(messageController.add, cancelOnError: true, onError: (e) {
        messageController.addError(e);
        channel.sink.close();
      });

When I purposefully use a wrong url. An exception is thown in the IOWebSocketChannel.connect function.

"Connection to '<URL>' was not upgraded to websocket"

Another Error

SocketException: Failed host lookup: 'URL' (OS Error: nodename nor servname provided, or not known, errno = 8)

The issue becomes that when these errors occur we handle these errors. However, even after the errors are handled the app still crashes.

khomin commented 1 year ago

Below is the implementation I am using for handling web socket requests.

WebSocketChannel.connect(Uri.parse(endpoint))

      final messageController = StreamController(onCancel: () {
        channel.sink.close();
      });

      channel.stream.listen(messageController.add, cancelOnError: true, onError: (e) {
        messageController.addError(e);
        channel.sink.close();
      });

When I purposefully use a wrong url. An exception is thown in the IOWebSocketChannel.connect function.

"Connection to '<URL>' was not upgraded to websocket"

Another Error

SocketException: Failed host lookup: 'URL' (OS Error: nodename nor servname provided, or not known, errno = 8)

The issue becomes that when these errors occur we handle these errors. However, even after the errors are handled the app still crashes.

Because the exceptions are inside the lib I've struggled a lot with that on desktop/web There are unhandled errors almost on every trivial case (like server is not responding) I came with idea to find another library or write my own

leonardopivetta commented 1 year ago

Same issue, using the WebSocketChannel.connect() method throws errors even if wrapped inside a try/catch block

JadKHaddad commented 1 year ago

Same issue, using the WebSocketChannel.connect() method throws errors even if wrapped inside a try/catch block

Same

JadKHaddad commented 1 year ago
WebSocketChannel channel = WebSocketChannel.connect(
    Uri.parse('ws://localhost:8000/ws'),
);

try {
    await channel.ready;
} catch (e) {
    print("Exception: $e");
}
aimer63 commented 3 months ago
WebSocketChannel channel = WebSocketChannel.connect(
    Uri.parse('ws://localhost:8000/ws'),
);

try {
    await channel.ready;
} catch (e) {
    print("Exception: $e");
}

If the server is down this will print something like

 WebSocketChannelException: Instance of 'WebSocketException'

I didn't figure out how to extract more information about the exception, someone knows? Even doing this:

try {
  await _channel.ready;
} catch(e) {
  final we = e as WebSocketChannelException;
  final we1 = we.inner as WebSocketException;
  print('${we1.message}');
}

it spits :

Unhandled Exception: type 'WebSocketException' is not a subtype of type 'WebSocketException' in type cast where
  WebSocketException is from package:web_socket/src/web_socket.dart  
  WebSocketException is from dart:_http

What I'm missing?