Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DioCacheInterceptor breaks error handling #148

Closed
fischermario opened this issue Mar 27, 2024 · 2 comments
Closed

DioCacheInterceptor breaks error handling #148

fischermario opened this issue Mar 27, 2024 · 2 comments

Comments

@fischermario
Copy link

The onError handler of DioCacheInterceptor is not able to hand over error handling to the next interceptor in the pipeline. After the onError handler is finished an exception occurs that is not cought by the subsequent interceptor(s).

Please consider the following example:

late CacheOptions cacheOptions;
late CacheStore cacheStore;
Dio dio = Dio(BaseOptions(
  receiveTimeout: const Duration(milliseconds: 2000),
  connectTimeout: const Duration(milliseconds: 2000),
  sendTimeout: const Duration(milliseconds: 2000),
  receiveDataWhenStatusError: true,
));
getTemporaryDirectory().then((dir) async {
  cacheStore = FileCacheStore(dir.path);
  cacheOptions = CacheOptions(
    store: cacheStore,
    // We will use cache functionality on demand
    policy: CachePolicy.noCache,
  );
  dio.interceptors.addAll([
    DioCacheInterceptor(
      options: cacheOptions,
    ),
    InterceptorsWrapper(
      onError: (err, handler) {
        debugPrint('CAUGHT error: $err');
        handler.resolve(
          Response(
            requestOptions: err.requestOptions,
            statusCode: 500,
          ),
        );
      },
    ),
  ]);
  Response resp = await dio.get('https://httpbin.org/delay/10');
  debugPrint(resp.statusCode.toString());
}

Changing this line to

class DioCacheInterceptor extends QueuedInterceptor {

fixes this problem (I can create a PR if interested).

I have also informed the Dio project about this behavior here.

Is there a particular reason why you chose to extend "Interceptor" instead of "QueuedInterceptor"?

@llfbandit
Copy link
Owner

I don't use QueuedInterceptor because of concurrency concern. There's no need to stream each request because of this package.
That said, dio should follow interceptor chain setup...

@kuhnroyal
Copy link

kuhnroyal commented Mar 28, 2024

Edit: Meant to post in the dio issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants