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 {
  if (!_shouldRetry(err)) {
    return handler.next(err);
  }

  final requestOptions = err.requestOptions;
  final retryCount = (requestOptions.extra['_gp_retry_count'] as int?) ?? 0;
  if (retryCount >= _options.maxRetries) {
    return handler.next(err);
  }

  final delay = _calculateDelay(retryCount);
  await Future.delayed(delay);

  requestOptions.extra['_gp_retry_count'] = retryCount + 1;

  try {
    final response = await _dio.fetch(requestOptions);
    return handler.resolve(response);
  } on DioException catch (retryErr) {
    return handler.next(retryErr);
  } catch (_) {
    return handler.next(err);
  }
}