onError method

  1. @override
void onError(
  1. DioException err,
  2. ErrorInterceptorHandler handler
)

Called when an exception was occurred during the request.

Implementation

@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
  final statusCode = err.response?.statusCode;

  if (statusCode == HttpStatus.unauthorized) {
    if (AuthInterceptorState.consecutiveRefreshFailures >=
        AuthInterceptorState.maxConsecutiveRefreshFailures) {
      return handler.reject(err);
    }

    final inFlight = AuthInterceptorState.refreshInFlight;
    if (inFlight != null) {
      await inFlight;
    } else {
      AuthInterceptorState.refreshInFlight = QGami.refreshAccessToken();
      final token = await AuthInterceptorState.refreshInFlight;

      if (token == null || token.isEmpty) {
        AuthInterceptorState.consecutiveRefreshFailures += 1;
      } else {
        AuthInterceptorState.consecutiveRefreshFailures = 0;
      }

      AuthInterceptorState.refreshInFlight = null;
    }
  }

  return handler.reject(err);
}