Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Question] Dio interceptor with retry not triggered when timeout() is invoked #442

@adummy832

Description

@adummy832

Hello, @knaeckeKami, i have this very basic app that uses dio and gql.
My problem is that the interceptor for retry is not being triggered.

I just wanted to ask if you have any idea of what am i doing wrong?
Also, would greatly appreciate if you could point me into some examples
on how to properly implement this Retry? Thanks!

Please refer to the code below.

import 'dart:async';

import 'package:dio/dio.dart';
import 'package:dio_smart_retry/dio_smart_retry.dart';
import 'package:ferry/ferry.dart';
import 'package:gql_dio_link/gql_dio_link.dart';

class DioClientManager {
  late final Dio _dio;
  late final Link _dioLink;

  DioClientManager() {
    const baseUrl = 'https://randomuser.me/api/?results=20&gender=female,male';

    final options = BaseOptions(
      baseUrl: baseUrl,
      connectTimeout: const Duration(seconds: 10),
      receiveTimeout: const Duration(seconds: 10),
    );

    _dio = Dio(options);

    _dio.interceptors.add(
      RetryInterceptor(
        dio: _dio,
        logPrint: print,
        retryEvaluator: (error, attempt) {
          /// Error/exception not being triggered ???
          /// Need help !!!

          print('$error');
          print('${error.message}');

          return true;
        },
      ),
    );

    _dioLink = Link.from([
      DioLink(baseUrl, client: _dio),
    ]);
  }

  Dio get client => _dio;

  Link get link => _dioLink;
}

class FerryClient extends Client {
  FerryClient({
    required Link link,
    required Cache cache,
  }) : super(link: link, cache: cache);

  @override
  Stream<OperationResponse<TData, TVars>> request<TData, TVars>(
    OperationRequest<TData, TVars> request, [
    NextTypedLink<TData, TVars>? forward,
  ]) async* {
    /// Set a faster duration, must handled on Retry Interceptor ???
    /// Need help !!!
    
    const duration = Duration(milliseconds: 100);

    final stream = super.request(request, forward).timeout(
      duration,
      onTimeout: (EventSink<OperationResponse<TData, TVars>> sink) {
        // Return error on timeout
        final error = DioException(
          requestOptions: RequestOptions(),
          message: 'My beautiful error message',
        );

        sink.addError(error);
        sink.close();
      },
    );

    yield* stream;
  }
}

void main() {
  final dioClientMgr = DioClientManager();
  final ferryClient = FerryClient(
    link: dioClientMgr.link,
    cache: Cache(),
  );
  
  /// Set some random request object
  final req = OperationRequest(...);
  ferryClient.request(req);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions