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

CancelableOperation value is not propagating errors, so they cannot be catched and app is crashing #246

Closed
busslina opened this issue Jun 16, 2023 · 3 comments

Comments

@busslina
Copy link

This problem began with Dio library on this issue.

Basically the thing is that be have a CancelableOperation being constructed from a Future value.

A) If we manage this future possible errors before constructing the CancelableOperation , it works fine: errors can be catched.

final operation = CancelableOperation.fromFuture(_fetch(
      options,
      requestStream,
      cancelFuture,
    ).catchError((error) {
      print('IOHttpClientAdapter.fetch() -- Future.catchError() -- $error');
    }));
    if (cancelFuture != null) {
      cancelFuture.whenComplete(() => operation.cancel());
    }
    return operation.value;

B) Then the trouble, if we manage the future possible errors after CancelableOperation being constructed, via its value property, errors are not catched and app is crashing.

final operation = CancelableOperation.fromFuture(_fetch(
      options,
      requestStream,
      cancelFuture,
    ));
    if (cancelFuture != null) {
      cancelFuture.whenComplete(() => operation.cancel());
    }
    return operation.value.catchError((error) {
      print('IOHttpClientAdapter.fetch() -- Future.catchError() -- $error');
    });

Is it the normal behaviour?
It would not be preferable to propagate also the future's error?

@busslina
Copy link
Author

busslina commented Jun 16, 2023

UPDATE I

In order to illustrate the issue with a test:

import 'package:async/async.dart';

void main(List<String> arguments) async {
  // _test1();
  // _test2();
  // await _test3();
}

void _test1() {
  final op = CancelableOperation.fromFuture(_getFutureError());

  op.value.then(
    (value) {
      print('Future value: $value');
    },
  ).catchError(
    (error) {
      print('Future catched error: $error');
      return null;
    },
  );
}

void _test2() {
  final op = CancelableOperation.fromFuture(
    _getFutureError().catchError(
      (error) {
        print('Future catched error: $error');
        return null;
      },
    ),
  );

  op.value.then((value) {
    print('Future value: $value');
  });
}

Future<void> _test3() async {
  final op = CancelableOperation.fromFuture(_getFutureError());

  try {
    final value = await op.value;
    print('Future value: $value');
  } catch (e) {
    print('Future catched error: $e');
  }
}

Future<String?> _getFutureError() async {
  // 5 seconds delay
  await Future.delayed(const Duration(seconds: 5));

  // Throwing error
  throw ('Manual triggered test error');
}

_test1() result:
imagen

_test2() result:
imagen

_test3() result:
imagen

@busslina
Copy link
Author

UPDATE II

Also not working using CancelableOperation.then method with onError argument. Giving same result: unhandled exception

@busslina
Copy link
Author

I'm closing due to this issue is related to VSCode and not the plugin:

dart-lang/sdk#47692
dart-lang/sdk#47985
https://docs.flutter.dev/testing/errors#errors-not-caught-by-flutter

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

1 participant