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

Can not catch cancellation exception #2028

Closed
tom-xy opened this issue Nov 11, 2023 · 2 comments
Closed

Can not catch cancellation exception #2028

tom-xy opened this issue Nov 11, 2023 · 2 comments
Labels
i: duplicate This issue or pull request already exists

Comments

@tom-xy
Copy link

tom-xy commented Nov 11, 2023

Package

dio

Version

5.3.3

Operating-System

MacOS

Output of flutter doctor -v

[✓] Flutter (Channel stable, 3.13.9, on macOS 14.1.1 23B81 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.13.9 on channel stable at /Users/tom/Library/Flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d211f42860 (2 weeks ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/tom/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = /Users/tom/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A240d
    ✗ Unable to get list of installed Simulator runtimes.
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (3 available)
    • Pixel 4 (mobile) • 98111FFAZ000UV • android-arm64  • Android 13 (API 33)
    • macOS (desktop)  • macos          • darwin-arm64   • macOS 14.1.1 23B81 darwin-arm64
    • Chrome (web)     • chrome         • web-javascript • Google Chrome 119.0.6045.123

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Dart Version

No response

Steps to Reproduce

import 'package:dio/dio.dart';
import 'package:path/path.dart' as p;
import 'dart:io';

// https://pub.dev/packages/dio#cancellation

final cancelToken = CancelToken();

void main(List<String> arguments) async {
  download();
  Future.delayed(Duration(seconds: 2)).then((value) {
    cancel();
  });
}

download() async {
  final dio = Dio();
  final url = "https://www.example.com/picture.png"; // For example
  final savePath = p.join(Directory.current.path, "picture.png");

  try {
    await dio.downloadUri(
      Uri.parse(url),
      savePath,
      cancelToken: cancelToken,
      onReceiveProgress: (int count, int total) async {
        print("Received $count, Total received: $total");
      },
    );
  } catch (e) {
    print("Error: $e");
  }
}

cancel() {
  try {
    cancelToken.cancel(); // Not an async method
  } catch (e) {
    print("$e");
  }
}

Expected Result

Cancellation exception caught

Actual Result

Cancellation exception not caught

@tom-xy tom-xy added h: need triage This issue needs to be categorized s: bug Something isn't working labels Nov 11, 2023
@tomekit

This comment was marked as off-topic.

@AlexV525
Copy link
Member

Duplicate of #1869

@AlexV525 AlexV525 marked this as a duplicate of #1869 Nov 13, 2023
@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
@AlexV525 AlexV525 added i: duplicate This issue or pull request already exists and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants