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 {
  final options = err.requestOptions;
  final retryCount = _nonNegativeInt(options.extra[retryCountKey]);

  if (!canRetry(err)) {
    handler.next(err);
    return;
  }

  final origin = options.uri.toString();
  if (!LikeConnectivityManager().isOriginAvailable(origin)) {
    handler.next(err);
    return;
  }

  try {
    await _cancellableDelay(
      policy.jitter(_delayForAttempt(options, retryCount)),
      options.cancelToken,
    );
    if (options.cancelToken?.isCancelled ?? false) {
      handler.next(err);
      return;
    }

    options.extra[retryCountKey] = retryCount + 1;
    final response = await dio.fetch<dynamic>(options);
    handler.resolve(response);
  } on DioException catch (retryError) {
    handler.next(retryError);
  } catch (error, stackTrace) {
    handler.next(
      DioException(
        requestOptions: options,
        error: error,
        stackTrace: stackTrace,
      ),
    );
  }
}