onError method

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

Called when an exception was occurred during the request.

Implementation

@override
Future<void> onError(DioException err, ErrorInterceptorHandler handler) async {
  var retries = err.requestOptions.extra['retries'] ?? 0;

  if (retries < maxRetries && _shouldRetry(err)) {
    retries++;
    err.requestOptions.extra['retries'] = retries;

    await Future.delayed(retryInterval);

    try {
      final response = await dio.fetch(err.requestOptions);
      return handler.resolve(response);
    } on DioException catch (e) {
      return super.onError(e, handler);
    }
  }

  return super.onError(err, handler);
}