I'm building a flutter app that uses SignalR. For this purpose I'm using signalr_core library. So far things work well when the server is up and running and I provide a valid authentication token, but when I provided a wrong token and start the connection I get the following error:
Error: Expected a value of type 'Error', but got one of type 'WebSocketChannelException'
at Object.throw_ [as throw] (http://localhost:64659/dart_sdk.js:4348:11)
at Object.castError (http://localhost:64659/dart_sdk.js:4319:15)
at Object.cast [as as] (http://localhost:64659/dart_sdk.js:4635:17)
at dart.LegacyType.new.as (http://localhost:64659/dart_sdk.js:6212:60)
at http://localhost:64659/packages/signalr_core/src/json_hub_protocol.dart.lib.js:2129:44
at _RootZone.runUnaryGuarded (http://localhost:64659/dart_sdk.js:37736:11)
at sendError (http://localhost:64659/dart_sdk.js:31344:26)
at _ControllerSubscription.new.[_sendError] (http://localhost:64659/dart_sdk.js:31359:11)
at _ControllerSubscription.new.[_addError] (http://localhost:64659/dart_sdk.js:31282:27)
at _SyncStreamController.new.[_sendError] (http://localhost:64659/dart_sdk.js:34230:39)
at _SyncStreamController.new.[_addError] (http://localhost:64659/dart_sdk.js:34042:27)
at _SyncStreamController.new.addError (http://localhost:64659/dart_sdk.js:34012:24)
at _RootZone.runBinaryGuarded (http://localhost:64659/dart_sdk.js:37753:11)
at sendError (http://localhost:64659/dart_sdk.js:31342:26)
at _ControllerSubscription.new.[_sendError] (http://localhost:64659/dart_sdk.js:31359:11)
at _ControllerSubscription.new.[_addError] (http://localhost:64659/dart_sdk.js:31282:27)
at _SyncStreamController.new.[_sendError] (http://localhost:64659/dart_sdk.js:34230:39)
at _SyncStreamController.new.[_addError] (http://localhost:64659/dart_sdk.js:34042:27)
at _SyncStreamController.new.addError (http://localhost:64659/dart_sdk.js:34012:24)
at _StreamSinkWrapper.new.addError (http://localhost:64659/dart_sdk.js:34339:24)
at _GuaranteeSink.new.[_addError] (http://localhost:64659/packages/stream_channel/src/stream_channel_controller.dart.lib.js:1223:26)
at _GuaranteeSink.new.addError (http://localhost:64659/packages/stream_channel/src/stream_channel_controller.dart.lib.js:1218:24)
at http://localhost:64659/packages/web_socket_channel/html.dart.lib.js:199:36
at _RootZone.runUnary (http://localhost:64659/dart_sdk.js:37810:58)
at _FutureListener.then.handleValue (http://localhost:64659/dart_sdk.js:32771:29)
at handleValueCallback (http://localhost:64659/dart_sdk.js:33319:49)
at Function._propagateToListeners (http://localhost:64659/dart_sdk.js:33357:17)
at _Future.new.[_complete] (http://localhost:64659/dart_sdk.js:33190:25)
at Object._cancelAndValue (http://localhost:64659/dart_sdk.js:38220:24)
at http://localhost:64659/dart_sdk.js:18845:17
at Object._checkAndCall (http://localhost:64659/dart_sdk.js:4558:16)
at Object.dcall (http://localhost:64659/dart_sdk.js:4563:17)
at WebSocket.<anonymous> (http://localhost:64659/dart_sdk.js:104962:23)
Although I did catch the exception in my code.
Here is the connection initialization code:
_connection = HubConnectionBuilder()
.withAutomaticReconnect() //
.withUrl(
_URL,
HttpConnectionOptions(
logging: (level, message) => print(message),
skipNegotiation: true,
transport: HttpTransportType.webSockets,
accessTokenFactory: () {
return Future.value('wrongToken');
},
)) //
.build(); //
As for connection start code:
void start() async {
try {
_streamingStateController.add(StreamingInfo(StreamingState.Starting));
await _connection.start();
_listenToGPSChange();
_streamingStateController.add(StreamingInfo(StreamingState.Started));
} on Exception catch (exception) {
_handleError('Oops. Unable to connect to the server.');
} on Error catch (error) {
_handleError('Oops. Unable to connect to the server.');
}
}
Please not that I get the same error when the connection succeeds (in case of valid parameters) but afterwards a shut down my server and send a message.
So the question is, how do I catch this type of errors?