onError method

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

Called when an exception was occurred during the request.

Implementation

@override
void onError(
  DioException err,
  ErrorInterceptorHandler handler,
) async {
  final cacheOptions = _getCacheOptions(err.requestOptions);

  if (_shouldSkip(err.requestOptions, options: cacheOptions, error: err)) {
    handler.next(err);
    return;
  }

  if (_isCacheCheckAllowed(err.response, cacheOptions)) {
    // Retrieve response from cache
    final cacheResponse = await _loadResponse(err.requestOptions);

    if (err.response != null && cacheResponse != null) {
      // Update cache response with response header values
      await _saveResponse(
        cacheResponse..updateCacheHeaders(err.response),
        cacheOptions,
        statusCode: err.response?.statusCode,
      );
    }

    // Resolve with found cached response
    if (cacheResponse != null) {
      handler.resolve(cacheResponse);
      return;
    }
  }

  handler.next(err);
}