dart-lang / web_socket_channel

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

Some Exceptions cannot be Handled #280

Closed Nabinda closed 1 year ago

Nabinda commented 1 year ago

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)

When the device is not connected on internet then this issues occurs which cannot be handled

natebosch commented 1 year ago

What have you tried to handle this exception? Do you have a reproduction you can share?

Nabinda commented 1 year ago
try {
      socket = IOWebSocketChannel.connect(SocketConfig.mainUrl);
      _messageController = StreamController<dynamic>.broadcast();
      socket.stream.listen(
        _onMessageReceived,
        onError: onError,
        onDone: onDone,
      );
    } catch (e) {
      onError(e);
    }

This is how I was trying to handle the issue however it was not working, this issue occurs when you try to connect the socket when device is not connected to internet

brianquinlan commented 1 year ago

@Nabinda could you try?

try {
  socket = IOWebSocketChannel.connect(SocketConfig.mainUrl);
  await socket.ready;  // Show throw if the connection cannot be established.
} catch (e) {
  onError(e);
}
NaveenNirban commented 1 year ago

@Nabinda could you try?

try {
  socket = IOWebSocketChannel.connect(SocketConfig.mainUrl);
  await socket.ready;  // Show throw if the connection cannot be established.
} catch (e) {
  onError(e);
}

Thanks buddy, it worked for me in case of Server not available. I was intentionally trying to catch this exception but was not able to find any way to do it. It would be great,if any explanation is there regarding await socket.ready; and why traditional catch is not able to handle this.