-
Notifications
You must be signed in to change notification settings - Fork 1.7k
invalid_return_type_for_catch_error does not detect implicit null returns #49215
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
Comments
Somewhere in your program you are using // -- /tmp/x.dart ----
void main() async {
await Future<int>.error('error').catchError((e) => '');
} $ dart /tmp/x.dart
Unhandled exception:
Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type
#0 _FutureListener.handleError (dart:async/future_impl.dart:181:7)
#1 Future._propagateToListeners.handleError (dart:async/future_impl.dart:778:47)
#2 Future._propagateToListeners (dart:async/future_impl.dart:799:13)
#3 Future._completeError (dart:async/future_impl.dart:574:5)
#4 Future._asyncCompleteError.<anonymous closure> (dart:async/future_impl.dart:665:7)
#5 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#6 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#7 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:122:13)
#8 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:193:5)
$ dart analyze /tmp/x.dart
Analyzing x.dart... 0.5s
info • x.dart:2:54 • A value of type 'String' can't be returned by the 'onError' handler because it must be assignable to 'FutureOr<int>'. •
invalid_return_type_for_catch_error
1 issue found. /cc @lrhn |
I agree with @mraleph. We are deliberately a little permissive around That makes it very hard to improve the experience. |
Yes. The only place I use xxxRequest<void>(something).catchError(
(dynamic e, StackTrace? s) {
someHandlerHere();
},
); It did not return a wrong type (as I thought) but the arguments might be wrong here. We didn't see anything in the analyzer, so it became a runtime exception eventually. And yes, I removed once it occurred because I realized it's the exception from |
If the future's type is indeed Are you sure about the actual runtime-type of the future returned by |
So I rewrote the method from the left to the right then no exception occurred again, and I can confirm the issue was resolved. P.S. The rewrote is done before I wrote the issue. |
I am still surprised that analyzer does not highlight this issue. |
Do we want to file an analyzer issue here? ( @bwilkerson @srawlins ) |
Ah-ha. I see, the analyzer seems to only validate return statements. void main() async {
await Future<int>.error('error').catchError((e) { return null; });
await Future<int>.error('error').catchError((e) { });
} dart analyze /tmp/x.dart
Analyzing x.dart... 0.5s
info • x.dart:2:60 • A value of type 'Null' can't be returned by the 'onError' handler because it must be assignable to 'FutureOr<int>'. •
invalid_return_type_for_catch_error
1 issue found. Though actually both of these should produce an error in null-safety mode. It's definitely an analyzer issue. |
Future.catchError
reports unreadable stack traces
Updated the title and area accordingly. |
Well, my original proposal is a better stack trace, which seems unable to resolve currently? |
I've implemented the analyzer bit in https://dart-review.googlesource.com/c/sdk/+/247863, but it will take some time to land as I have to clean up Flutter first. |
This is similar to BODY_MIGHT_COMPLETE_NORMALLY (the static error), BODY_MIGHT_COMPLETE_NORMALLY_NULLABLE (the Hint for nullable return types), and RETURN_OF_INVALID_TYPE_FROM_CATCH_ERROR, combining the rules from each. #49215 (but maybe doesn't close, if the core issue is the VM issue) Change-Id: I9a010e5a3f0f88abfdab479339e5e76759ae9d92 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247863 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Ben Konyi <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
@srawlins Could you please also update documentation?
|
@bwilkerson would you like to take a stab at writing docs for
I've filed #50430. Please comment if I misunderstood your request here. |
The docs are already in the pipeline, waiting for a review from the documentation team. |
by converting the Future to void, seeing as the return value is not used anyway Change-Id: Ifb4fdc6f909bc0432427b0e58d59eb10a1c7a5cd Tested: only CI. Bug: #49215 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/269303 Reviewed-by: Daco Harkes <[email protected]> Commit-Queue: Oleh Prypin <[email protected]>
Hi, no news since November ? |
The documentation landed, so I'm not sure whether there's any work that still needs to be done. |
I think this is good to go then. Thanks @bwilkerson ! |
Uh oh!
There was an error while loading. Please reload this page.
This tracker is for issues related to Analyzer.
Summary
Some exceptions were thrown due to unknown reasons, we managed to catch them but the stack trace was unable to locate the root cause of the issue.
Expect behavior
Reports the precise cause of the error.
The text was updated successfully, but these errors were encountered: