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

unhandled cancel Exception #306

Closed
zhengbomo opened this issue May 20, 2019 · 9 comments
Closed

unhandled cancel Exception #306

zhengbomo opened this issue May 20, 2019 · 9 comments

Comments

@zhengbomo
Copy link

after cancel the request by token, dio has also occurred an unhandled Exception like this

unhandle_cancel_exception

here is my code

import 'package:dio/dio.dart';

...

void _testDio() async {
  CancelToken token = CancelToken();
  var url = "https://github.com/flutterchina12121/dio";

  Future.delayed(const Duration(milliseconds: 0), () async {
    try {
      Response response = await Dio().get(url, cancelToken: token);
      if (response.statusCode == 200) {
        print("request success");
      } else {
        print("request failed");
      }
    } catch (e) {
      print("request failed with exception");
    }
  });

  Future.delayed(const Duration(milliseconds: 250), () {
    print("manual cancel");
    if (!token.isCancelled) {
      token.cancel();
    }
  });
}

when i call _testDio frequently, the Exception occurred

version:

  • flutter: 1.5.4-hotfix.2
  • platform: iOS
  • dart: 2.3.0
@wendux
Copy link
Contributor

wendux commented May 21, 2019

Not bug! Dio will throw a DioError error with DioErrorType.CANCEL when request is cancelled.

@zhengbomo
Copy link
Author

Not bug! Dio will throw a DioError error with DioErrorType.CANCEL when request is cancelled.

@wendux is there any idea to fix it?

@zhengbomo zhengbomo changed the title unhandle cancel Exception unhandled cancel Exception May 21, 2019
@wendux
Copy link
Contributor

wendux commented May 24, 2019

you can refer to https://github.com/flutterchina/dio/blob/master/example/cancel_request.dart

@micsanbr
Copy link

dio.download(url, filePath, cancelToken: _cancelToken, /.../) still throws an exception when using catchError() after cancelling with the token.

the workaround seems to be a try/catch using DioError
try {} on DioError catch (exception) { print("something"); }

@ganeshrvel
Copy link

@micsan13br both
catchError and try {} on DioError catch (exception) { print("something"); } doesn't seem to work.

@rvn9
Copy link

rvn9 commented May 27, 2022

what to do if i only want to cancel once ? lets say im donwloading something and user want to manual cancel that donwload, when im calling token.cancel() for the next request auto exception, must keep making diffrent cancel token for each req ?

@Sp4Rx
Copy link

Sp4Rx commented Sep 7, 2022

if you want to cancel once assign a new cancelToken after the first request has been canceled.

    CancelToken _cancelToken = CancelToken();
   Response response = await _dio.get(
      url,
      options: Options(headers: headers),
      cancelToken: _cancelToken,
    );

    //After some operation
    _cancelToken.cancel('Logged out');
    //Some operation
    _cancelToken = CancelToken();

@testOccu16
Copy link

you can refer to https://github.com/flutterchina/dio/blob/master/example/cancel_request.dart

404 - Page Not Found

@choijaegwon
Copy link

choijaegwon commented Jan 14, 2025

취소처리될때 에러 반환 하지 않는방법

} catch (e) {
    if (e is DioException && e.type == DioExceptionType.cancel) {
        // 필요시 취소처리 로직추가
        return;
    }

    // 에러처리 로직
}

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

8 participants