There have been multiple times when I’ve accidentally written code like this:
Future<Foo> doSomethingUsefulAndGetFoo() async {
try {
/* do something useful */
return getFooButMayFail(); // wrong!
} on GettingFooFailedException {
/* do something with the exception */
rethrow;
}
}
While the right code would look like so:
Future<Foo> doSomethingUsefulAndGetFoo() async {
try {
/* do something useful */
return await getFooButMayFail();
} on GettingFooFailedException {
/* do something with the exception */
rethrow;
}
}
The problem can be somewhat hard to spot: we are returning a Future<Foo> from the function instead of awaiting it and returning the result. In the first case, the exception does not reach the catch block, causing it to be thrown outside without the intended handling. Even when the goal is only to catch and not rethrow, this remains an issue—and I can’t recall ever writing this intentionally.
I thought it would be great if there were a linter rule for this, but since there isn’t, I’m submitting this feature request.
There have been multiple times when I’ve accidentally written code like this:
While the right code would look like so:
The problem can be somewhat hard to spot: we are returning a
Future<Foo>from the function instead ofawaiting it and returning the result. In the first case, the exception does not reach the catch block, causing it to be thrown outside without the intended handling. Even when the goal is only to catch and not rethrow, this remains an issue—and I can’t recall ever writing this intentionally.I thought it would be great if there were a linter rule for this, but since there isn’t, I’m submitting this feature request.