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 {
  var options = err.requestOptions;
  // 获取缓存模式
  CacheMode cacheMode = _getCacheMode(options);
  if (CacheMode.NETWORK_PUT_READ_CACHE == cacheMode) {
    // 如果缓存模式是优先网络则尝试获取缓存
    // 如果缓存模式是优先缓存,则先校验缓存
    var cache = await CacheManager.getInstance().getCacheWithReq(options);
    if (cache == null ||
        cache.expireTime <= DateTime.now().millisecondsSinceEpoch) {
      // 如果没有缓存或者缓存过期了
      return super.onError(err, handler);
    }

    return handler.resolve(_buildResponse(cache, options));
  }
  return super.onError(err, handler);
}