onError method

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

The callback will be executed on error.

If you want to continue the error , call handler.next.

If you want to complete the response with some custom data directly, you can resolve a Response object with handler.resolve and other error interceptor(s) will be skipped.

If you want to complete the response with an error message directly, you can reject a DioError object with handler.reject, and other error interceptor(s) will be skipped.

Implementation

@override
void onError(DioError err, ErrorInterceptorHandler handler) async {
  var dioCacheOptions =
      MapHelper.getDioCacheOptionsFromExtras(err.requestOptions) ??
          globalDioCacheOptions;

  if (dioCacheOptions?.httpCacheOnError != null) {
    bool isError = dioCacheOptions?.httpCacheOnError!(err) ?? false;

    if (isError) {
      String? data = await Fnc()
          .baseDb
          .getResponseData(key: _getStorageUrl(err.requestOptions));

      if (data != null) {
        return handler.resolve(Response(
            requestOptions: err.requestOptions, data: json.decode(data)));
      }
      return super.onError(err, handler);
    } else {
      return super.onError(err, handler);
    }
  } else {
    super.onError(err, handler);
  }
}