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

DioInterceptor throwing GET request is not handled #103

Open
LocaFoxNick opened this issue May 19, 2021 · 1 comment
Open

DioInterceptor throwing GET request is not handled #103

LocaFoxNick opened this issue May 19, 2021 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@LocaFoxNick
Copy link

Description

Using the DioInterceptor, should a request throw a DioError, other interceptors are not informed.

dio_interceptor.dart contains the method void onRequest(options, handler) async, and the following is the issue:

if (isError(response)) {
  handler.reject(response as DioError);
  return;
}

reject should be passing callFollowingErrorInterceptor = true

void reject(DioError error, [bool callFollowingErrorInterceptor = false])
package:dio/src/interceptor.dart

Complete the request with an error! Other request/response interceptor(s) will not be executed, but error interceptor(s) may be executed, which depends on whether the value of parameter [callFollowingErrorInterceptor] is true.

[error]: Error info to reject. [callFollowingErrorInterceptor]: Whether to call the error interceptor(s).

Stacktrace:

DioError [DioErrorType.response]: {message: Some beautiful error!}
  dart:async/future_impl.dart 25:44                                      _Completer.completeError
  package:dio/src/interceptor.dart 120:16                                RequestInterceptorHandler.reject
  package:http_mock_adapter/src/interceptors/dio_interceptor.dart 67:15  DioInterceptor.onRequest
  package:dio/src/dio_mixin.dart 502:28                                  DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>.<fn>
  package:dio/src/dio_mixin.dart 795:22                                  DioMixin.checkIfNeedEnqueue
  package:dio/src/dio_mixin.dart 500:22                                  DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>
  dart:async/future.dart 174:37                                          new Future.<fn>
  package:stack_trace/src/stack_zone_specification.dart 208:15           StackZoneSpecification._run
  package:stack_trace/src/stack_zone_specification.dart 116:48           StackZoneSpecification._registerCallback.<fn>
  dart:async/zone.dart 1346:47                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1162:7                                            _CustomZone.runGuarded
  dart:async/zone.dart 1202:23                                           _CustomZone.bindCallbackGuarded.<fn>
  package:stack_trace/src/stack_zone_specification.dart 208:15           StackZoneSpecification._run
  package:stack_trace/src/stack_zone_specification.dart 116:48           StackZoneSpecification._registerCallback.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1186:23                                           _CustomZone.bindCallback.<fn>
  dart:async-patch/timer_patch.dart 18:15                                Timer._createTimer.<fn>
  dart:isolate-patch/timer_impl.dart 395:19                              _Timer._runTimers
  dart:isolate-patch/timer_impl.dart 426:5                               _Timer._handleMessage
  dart:isolate-patch/isolate_patch.dart 184:12                           _RawReceivePortImpl._handleMessage
  ===== asynchronous gap ===========================
  dart:async/zone.dart 1279:19                                           _CustomZone.registerCallback
  dart:async/zone.dart 1201:22                                           _CustomZone.bindCallbackGuarded
  dart:async/timer.dart 48:45                                            new Timer
  dart:async/timer.dart 81:9                                             Timer.run
  dart:async/future.dart 172:11                                          new Future
  package:dio/src/dio_mixin.dart 499:13                                  DioMixin.fetch._requestInterceptorWrapper.<fn>
  package:dio/src/dio_mixin.dart 494:14                                  DioMixin.fetch._requestInterceptorWrapper.<fn>
  package:stack_trace/src/stack_zone_specification.dart 126:26           StackZoneSpecification._registerUnaryCallback.<fn>.<fn>
  package:stack_trace/src/stack_zone_specification.dart 208:15           StackZoneSpecification._run
  package:stack_trace/src/stack_zone_specification.dart 126:14           StackZoneSpecification._registerUnaryCallback.<fn>
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  ===== asynchronous gap ===========================
  dart:async/zone.dart 1286:19                                           _CustomZone.registerUnaryCallback
  dart:async/future_impl.dart 293:23                                     Future.then
  package:dio/src/dio_mixin.dart 576:23                                  DioMixin.fetch.<fn>
  dart:collection/list.dart 86:13                                        ListMixin.forEach
  package:dio/src/dio_mixin.dart 575:18                                  DioMixin.fetch
  package:dio/src/dio_mixin.dart 466:12                                  DioMixin.request
  package:dio/src/dio_mixin.dart 53:12                                   DioMixin.get
  test\util\dio_service_test.dart 505:39                                 main.<fn>.<fn>.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async/zone.dart 1286:19                                           _CustomZone.registerUnaryCallback
  dart:async-patch/async_patch.dart 45:22                                _asyncThenWrapperHelper
  test\util\dio_service_test.dart                                        main.<fn>.<fn>.<fn>.<fn>
  package:test_api/src/backend/declarer.dart 200:19                      Declarer.test.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async/zone.dart 1286:19                                           _CustomZone.registerUnaryCallback
  dart:async-patch/async_patch.dart 45:22                                _asyncThenWrapperHelper
  package:test_api/src/backend/declarer.dart                             Declarer.test.<fn>.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1789:10                                           _runZoned
  dart:async/zone.dart 1711:10                                           runZoned
  package:test_api/src/backend/declarer.dart 198:13                      Declarer.test.<fn>
  package:test_api/src/backend/declarer.dart 181:48                      Declarer.test.<fn>
  package:test_api/src/backend/invoker.dart 231:15                       Invoker.waitForOutstandingCallbacks.<fn>
  package:test_api/src/backend/invoker.dart 228:14                       Invoker.waitForOutstandingCallbacks.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1789:10                                           _runZoned
  dart:async/zone.dart 1711:10                                           runZoned
  package:test_api/src/backend/invoker.dart 228:5                        Invoker.waitForOutstandingCallbacks
  package:test_api/src/backend/invoker.dart 383:17                       Invoker._onRun.<fn>.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async/zone.dart 1286:19                                           _CustomZone.registerUnaryCallback
  dart:async-patch/async_patch.dart 45:22                                _asyncThenWrapperHelper
  package:test_api/src/backend/invoker.dart                              Invoker._onRun.<fn>.<fn>.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1789:10                                           _runZoned
  dart:async/zone.dart 1711:10                                           runZoned
  package:test_api/src/backend/invoker.dart 370:9                        Invoker._onRun.<fn>.<fn>
  package:test_api/src/backend/invoker.dart 415:15                       Invoker._guardIfGuarded
  package:test_api/src/backend/invoker.dart 369:7                        Invoker._onRun.<fn>
  package:stack_trace/src/chain.dart 94:24                               Chain.capture.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1789:10                                           _runZoned
  dart:async/zone.dart 1711:10                                           runZoned
  package:stack_trace/src/chain.dart 92:12                               Chain.capture
  package:test_api/src/backend/invoker.dart 368:11                       Invoker._onRun
  package:test_api/src/backend/live_test_controller.dart 153:11          LiveTestController.run
  package:test_api/src/remote_listener.dart 255:16                       RemoteListener._runLiveTest.<fn>
  dart:async/zone.dart 1354:13                                           _rootRun
  dart:async/zone.dart 1258:19                                           _CustomZone.run
  dart:async/zone.dart 1789:10                                           _runZoned
  dart:async/zone.dart 1711:10                                           runZoned
  package:test_api/src/remote_listener.dart 254:5                        RemoteListener._runLiveTest
  package:test_api/src/remote_listener.dart 207:7                        RemoteListener._serializeTest.<fn>
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:async/zone.dart 1370:13                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:async/stream_controller.dart 826:13                               _StreamSinkWrapper.add
  package:stream_channel/src/guarantee_channel.dart 125:12               _GuaranteeSink.add
  package:stream_channel/src/multi_channel.dart 159:31                   new _MultiChannel.<fn>
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:_internal/async_cast.dart 85:11                                   CastStreamSubscription._onData
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:async/stream_controller.dart 826:13                               _StreamSinkWrapper.add
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_pipe.dart 123:11                                     _ForwardingStreamSubscription._add
  dart:async/stream_pipe.dart 218:10                                     _MapStream._handleData
  dart:async/stream_pipe.dart 153:13                                     _ForwardingStreamSubscription._handleData
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:_http/websocket_impl.dart 1145:21                                 new _WebSocketImpl._fromSocket.<fn>
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_transformers.dart 63:11                              _SinkTransformerStreamSubscription._add
  dart:async/stream_transformers.dart 13:11                              _EventSinkWrapper.add
  dart:_http/websocket_impl.dart 338:23                                  _WebSocketProtocolTransformer._messageFrameEnd
  dart:_http/websocket_impl.dart 232:46                                  _WebSocketProtocolTransformer.add
  dart:async/stream_transformers.dart 111:24                             _SinkTransformerStreamSubscription._handleData
  dart:async/zone.dart 1362:47                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:io-patch/socket_patch.dart 2160:41                                _Socket._onData
  dart:async/zone.dart 1370:13                                           _rootRunUnary
  dart:async/zone.dart 1265:19                                           _CustomZone.runUnary
  dart:async/zone.dart 1170:7                                            _CustomZone.runUnaryGuarded
  dart:async/stream_impl.dart 341:11                                     _BufferingStreamSubscription._sendData
  dart:async/stream_impl.dart 271:7                                      _BufferingStreamSubscription._add
  dart:async/stream_controller.dart 733:19                               _SyncStreamControllerDispatch._sendData
  dart:async/stream_controller.dart 607:7                                _StreamController._add
  dart:async/stream_controller.dart 554:5                                _StreamController.add
  dart:io-patch/socket_patch.dart 1696:33                                new _RawSocket.<fn>
  dart:io-patch/socket_patch.dart 1208:14                                _NativeSocket.issueReadEvent.issue
  dart:async/schedule_microtask.dart 40:21                               _microtaskLoop
  dart:async/schedule_microtask.dart 49:5                                _startMicrotaskLoop

  package:dio/src/dio_mixin.dart 618:7    DioMixin.fetch.<fn>
  ===== asynchronous gap ===========================
  dart:async                              Future.catchError
  package:dio/src/dio_mixin.dart 609:8    DioMixin.fetch
  package:dio/src/dio_mixin.dart 466:12   DioMixin.request
  package:dio/src/dio_mixin.dart 53:12    DioMixin.get
  test\util\dio_service_test.dart 505:39  main.<fn>.<fn>.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async                              _asyncThenWrapperHelper
  test\util\dio_service_test.dart         main.<fn>.<fn>.<fn>.<fn>

Steps to reproduce

Testing code:

//ARRANGE
final _dioAdapter = DioAdapter();
final _dio = Dio()..httpClientAdapter = _dioAdapter;

final _interceptor = DioInterceptor();
_dio.interceptors.add(_interceptor);

final firstRequestHeaders = {
  'accept-language': 'en',
  'accept': 'application/json',
  'content-type': 'application/json',
  'authorization': 'bearer ${accessToken.token}',
};

final error = getError(401, '/Test', firstRequestHeaders);

_interceptor
  ..onGet(
    '/Test',
    (request) => request.throws(
      401,
      error,
    ),
    headers: firstRequestHeaders,
  )
  ..onGet(
    '/Test',
    (request) => request.reply(200, ''),
    headers: {
      'accept-language': 'en',
      'accept': 'application/json',
      'content-type': 'application/json',
      'authorization': 'bearer ${refreshedAccessToken.token}',
    },
  )
  ..onError(error, ErrorInterceptorHandler());

//ACT
final response = await _dio.get('/Test');

Code being tested:

dio.interceptors.add(
    InterceptorsWrapper(onRequest: (requestOptions, handler) async {
      if (tenantId != null && accessToken != null) {
        requestOptions.headers['authorization'] = 'bearer ${accessToken!.token}';
      }

      handler.next(requestOptions);
    }, onError: (err, handler) async {
      if (tenantId != null && accessToken != null && err.response?.statusCode == HttpStatus.unauthorized) {
        dio.interceptors.requestLock.lock();
        dio.interceptors.responseLock.lock();

        final options = err.requestOptions;
        final refreshDio = getInstanceNoAuth();
        final newAccessToken = await _tokenRefresher.refresh(
          refreshDio: refreshDio,
          tenantId: tenantId,
          accessToken: accessToken!,
        );

        dio.interceptors.requestLock.unlock();
        dio.interceptors.responseLock.unlock();

        if (newAccessToken != null) {
          await _tenantLocalRepo.setAccessToken(newAccessToken);
          accessToken = newAccessToken;
          handler.resolve(await dio.fetch(options));
        } else {
          handler.next(err);
        }
      } else {
        handler.next(err);
      }
    }),
  );

Expected behavior

I expect any error interceptor to be called if there's an error thrown in the onGet request

System details

http_mock_adapter: ^0.2.1
dio: ^4.0.0

@LocaFoxNick LocaFoxNick added the bug Something isn't working label May 19, 2021
@GiorgiBeriashvili GiorgiBeriashvili added this to the Version 1.0.0 milestone Jun 5, 2021
@orestesgaolin
Copy link

Having the same issue when using fresh_dio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants