@@ -551,10 +551,12 @@ class TransportWsClientOptions {
551551class _Connected {
552552 final WebSocketChannel socket;
553553 final Future <void > throwOnClose;
554+ final Future <LikeCloseEvent ?> likeCloseEvent;
554555
555556 _Connected (
556557 this .socket,
557558 this .throwOnClose,
559+ this .likeCloseEvent,
558560 );
559561}
560562
@@ -879,9 +881,26 @@ class _ConnectionState {
879881 retries = 0 ; // reset the retries on connect
880882 final _completer = Completer <void >();
881883 errorOrClosed (_completer.completeError);
884+ // workground for dart linux bug: complete error not being caught by try..catch block
885+ final _likeCloseEventCompleter = Completer <LikeCloseEvent ?>();
882886 connected (_Connected (
883887 socket,
884- _completer.future,
888+ _completer.future.catchError ((err) {
889+ // workground for dart linux bug: complete error not being caught by try..catch block
890+ // if the connection is closed, the error is not fatal
891+ if (err is LikeCloseEvent ) {
892+ if (! _likeCloseEventCompleter.isCompleted) {
893+ _likeCloseEventCompleter.complete (err);
894+ }
895+ return ;
896+ }
897+ throw err;
898+ }).whenComplete (() {
899+ if (! _likeCloseEventCompleter.isCompleted) {
900+ _likeCloseEventCompleter.complete (null );
901+ }
902+ }),
903+ _likeCloseEventCompleter.future,
885904 ));
886905 } catch (err) {
887906 // stop reading messages as soon as reading breaks once
@@ -930,6 +949,7 @@ class _ConnectionState {
930949
931950 final socket = _connection.socket;
932951 final throwOnClose = _connection.throwOnClose;
952+ final likeCloseEvent = _connection.likeCloseEvent;
933953
934954 // if the provided socket is in a closing state, wait for the throw on close
935955 // TODO: WebSocketChannel should have a `state` getter
@@ -972,6 +992,7 @@ class _ConnectionState {
972992 // or
973993 throwOnClose,
974994 ]),
995+ waitForLikeCloseEvent: likeCloseEvent,
975996 );
976997 }
977998}
@@ -1013,6 +1034,7 @@ class _Client extends TransportWsClient {
10131034 final socket = _c.socket;
10141035 final release = _c.release;
10151036 final waitForReleaseOrThrowOnClose = _c.waitForReleaseOrThrowOnClose;
1037+ final waitForLikeCloseEvent = _c.waitForLikeCloseEvent;
10161038 // print("isolate debug name: ${Isolate.current.debugName}");
10171039 // print(payload.operation.toString());
10181040 // print(payload.variables.toString());
@@ -1072,6 +1094,12 @@ class _Client extends TransportWsClient {
10721094 // whatever happens though, we want to stop listening for messages
10731095 await waitForReleaseOrThrowOnClose.whenComplete (unlisten);
10741096
1097+ // workground for dart linux bug: complete error not being caught by try..catch block
1098+ final likeCloseEvent = await waitForLikeCloseEvent;
1099+ if (likeCloseEvent != null ) {
1100+ throw likeCloseEvent;
1101+ }
1102+
10751103 return ; // completed, shouldnt try again
10761104 } catch (errOrCloseEvent) {
10771105 if (! state.shouldRetryConnectOrThrow (errOrCloseEvent)) return ;
@@ -1123,11 +1151,13 @@ class _Connection {
11231151 final WebSocketChannel socket;
11241152 final Completer <void > release;
11251153 final Future <void > waitForReleaseOrThrowOnClose;
1154+ final Future <LikeCloseEvent ?> waitForLikeCloseEvent;
11261155
11271156 _Connection ({
11281157 required this .socket,
11291158 required this .release,
11301159 required this .waitForReleaseOrThrowOnClose,
1160+ required this .waitForLikeCloseEvent,
11311161 });
11321162}
11331163
0 commit comments