Skip to content

[flutter_tool] Where possible, catch only subtypes of Exception - #51440

Merged
zanderso merged 1 commit into
flutter:masterfrom
zanderso:catch-exception
Feb 27, 2020
Merged

zanderso merged 1 commit into
flutter:masterfrom
zanderso:catch-exception

Conversation

@zanderso

@zanderso zanderso commented Feb 25, 2020 •

Copy link
Copy Markdown
Member

Description

The pattern:

try {
  ...
} catch {
  ...
}

is problematic because the catch clause will catch not only exceptions resulting from usage and system errors (FileSystemException, SocketException, etc.), but also those resulting from programming errors (TypeError, AssertionError, NoSuchMethodError, etc.). In particular, the programming errors should be surfaced by tests and/or crash the tool with a report to crash logging.

This PR removes the above pattern. Unfortunately, I don't think we can enable the analyzer lint to ban it since there are a few places where it is needed, which I've noted.

Related Issues

#8742

Tests

I added the following tests:

A few erroneously passing tests fixed.

Breaking Change

Did any tests fail when you ran them? Please read [Handling breaking changes].

  • No, no existing tests failed, so this is not a breaking change.

@zanderso zanderso added tool Affects the "flutter" command-line tool. See also t: labels. work in progress; do not review labels Feb 25, 2020

@jonahwilliams jonahwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall change LGTM

Is there a lint (or regular expression) we could use to prevent us from regressing?

@jonahwilliams

This comment has been minimized.

@christopherfujino

Copy link
Copy Markdown
Contributor

@zanderso Could we enable the lint and ignore the specific exceptions we need?

@zanderso

Copy link
Copy Markdown
Member Author

@zanderso Could we enable the lint and ignore the specific exceptions we need?

I'll investigate that.

@christopherfujino christopherfujino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a lot of nits, getting rid of on Exception catch (_) is not that important, but if we use finally rather than catch -> rethrow, enabling the lint would be a lot easier.

Comment thread packages/flutter_tools/lib/src/cache.dart Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. we don't need to catch unused Exception

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit we don't need to catch unused Exception

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp cathcing -> catching

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this just be a finally rather than catch-rethrow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally over catch-rethrow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally over catch-rethrow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The finally clause would run even if no exception is thrown, which would change the semantics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit no need to catch unused Exception

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit no need to catch unused exception

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit no need to catch unused exception

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@christopherfujino christopherfujino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zanderso

Copy link
Copy Markdown
Member Author

@christopherfujino Thanks for the thorough review. I enabled the lint as well.

@zanderso
zanderso merged commit f1cd47e into flutter:master Feb 27, 2020
@christopherfujino

Copy link
Copy Markdown
Contributor

@christopherfujino Thanks for the thorough review. I enabled the lint as well.

Cool, I think this is a good change! I suspect we'll see a lot of things surface after this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants