From 755504f770a0d87a0d40beae0eab6eb5a14c6ada Mon Sep 17 00:00:00 2001 From: Dong Wook Kim <dehypnosis@gmail.com> Date: Sun, 19 Feb 2023 16:39:59 +0900 Subject: [PATCH 1/4] feat(gql_websocket_link)!: add connection error stream for client-side error handling --- links/gql_websocket_link/lib/src/link.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/links/gql_websocket_link/lib/src/link.dart b/links/gql_websocket_link/lib/src/link.dart index 8c2d03f5..ff8f31e1 100644 --- a/links/gql_websocket_link/lib/src/link.dart +++ b/links/gql_websocket_link/lib/src/link.dart @@ -125,6 +125,12 @@ class WebSocketLink extends Link { Stream<ConnectionState> get connectionStateStream => _connectionStateController.stream; + /// A stream that notifies about connection errors. + final StreamController<ConnectionError> _connectionErrorController = + StreamController<ConnectionError>.broadcast(); + Stream<ConnectionError> get connectionErrorStream => + _connectionErrorController.stream; + /// Initialize the [WebSocketLink] with a [uri]. /// You can customize the headers & protocols by passing [channelGenerator], /// if [channelGenerator] is passed, [uri] must be null. @@ -245,6 +251,8 @@ class WebSocketLink extends Link { ).catchError(_messagesController.addError); }); _reConnectRequests.clear(); + } else if (parsedMessage is ConnectionError) { + _connectionErrorController.add(parsedMessage); } }, onDone: () { if (isDisabled) { From 0001f12e159b59b2ee888797e375a10169fb11f2 Mon Sep 17 00:00:00 2001 From: Dong Wook Kim <dehypnosis@gmail.com> Date: Sun, 19 Mar 2023 02:18:36 +0900 Subject: [PATCH 2/4] fix(gql_websocket_link)!: don't close message subscriptions on WebSocketChannelException when autoReconnect is set --- links/gql_websocket_link/lib/src/link.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/links/gql_websocket_link/lib/src/link.dart b/links/gql_websocket_link/lib/src/link.dart index ff8f31e1..a314761e 100644 --- a/links/gql_websocket_link/lib/src/link.dart +++ b/links/gql_websocket_link/lib/src/link.dart @@ -274,6 +274,10 @@ class WebSocketLink extends Link { _close(); } }, onError: (Object error) { + if(autoReconnect && error is WebSocketChannelException) { + _connectionErrorController.add(ConnectionError(error)); + return; + } _messagesController.addError(error); }); From 73261d96c759efe85f3c2f0e33d8a48a5e4bb3f9 Mon Sep 17 00:00:00 2001 From: Dong Wook Kim <dehypnosis@gmail.com> Date: Fri, 24 Mar 2023 09:15:58 +0900 Subject: [PATCH 3/4] fix(gql_websocket_link)!: dispose _connectionErrorController on diposing link --- links/gql_websocket_link/lib/src/link.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/links/gql_websocket_link/lib/src/link.dart b/links/gql_websocket_link/lib/src/link.dart index a314761e..557ccfb5 100644 --- a/links/gql_websocket_link/lib/src/link.dart +++ b/links/gql_websocket_link/lib/src/link.dart @@ -417,6 +417,7 @@ class WebSocketLink extends Link { await _channel?.sink.close(websocket_status.normalClosure); _connectionStateController.add(ConnectionState.closed); await _connectionStateController.close(); + await _connectionErrorController.close(); await _messagesController.close(); _disposedCompleter!.complete(); } From 1d72c812e273d4eb5b29a6a9e66472521fd2654d Mon Sep 17 00:00:00 2001 From: Dong Wook Kim <dehypnosis@gmail.com> Date: Fri, 24 Mar 2023 09:36:06 +0900 Subject: [PATCH 4/4] fix(gql_websocket_link)!: reformat code --- links/gql_websocket_link/lib/src/link.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/links/gql_websocket_link/lib/src/link.dart b/links/gql_websocket_link/lib/src/link.dart index 557ccfb5..1868755d 100644 --- a/links/gql_websocket_link/lib/src/link.dart +++ b/links/gql_websocket_link/lib/src/link.dart @@ -274,7 +274,7 @@ class WebSocketLink extends Link { _close(); } }, onError: (Object error) { - if(autoReconnect && error is WebSocketChannelException) { + if (autoReconnect && error is WebSocketChannelException) { _connectionErrorController.add(ConnectionError(error)); return; }