handleError method

Future<void> handleError(
  1. XmppError error
)

Called when a stream ending error has occurred

Implementation

Future<void> handleError(XmppError error) async {
  _log.severe('handleError called with $error');

  // Whenever we encounter an error that would trigger a reconnection attempt while
  // the connection result is being awaited, don't attempt a reconnection but instead
  // try to gracefully disconnect.
  if (_connectionCompleter != null) {
    _log.info(
      'Not triggering reconnection since connection result is being awaited',
    );
    await _disconnect(
      triggeredByUser: false,
      state: XmppConnectionState.error,
    );
    _connectionCompleter?.complete(
      Result(
        error,
      ),
    );
    _connectionCompleter = null;
    return;
  }

  // Close the socket
  _socket.close();

  if (!error.isRecoverable()) {
    // We cannot recover this error
    _log.severe(
      'Since a $error is not recoverable, not attempting a reconnection',
    );
    await _setConnectionState(XmppConnectionState.error);
    await _sendEvent(
      NonRecoverableErrorEvent(error),
    );
    return;
  }

  // The error is recoverable
  await _setConnectionState(XmppConnectionState.notConnected);

  if (await _reconnectionPolicy.canTriggerFailure()) {
    await _reconnectionPolicy.onFailure();
  } else {
    _log.info(
      'Not passing connection failure to reconnection policy as it indicates that we should not reconnect',
    );
  }
}