Skip to content

Add a lint for avoiding .catchError() #58218

Closed
@DanTup

Description

@DanTup

An issue that's been coming up more and more recently in VS Code is the debugger breaking on exceptions that the user considers "caught" but the Dart VM does not. For example this code:

Future<void> main() async {
  await throwErr().catchError((e) => print('Caught!'));
}

Future<void> throwErr() async {
  throw 'a';
}

Here, when the exception is thrown, the VM examines the stack and sees no catch block so considers the error uncaught. This breaks the debugger (when using the default "break on uncaught exceptions" mode) and is very confusing (it's not clear to the user if there's some uncaught error they need to handle).

There are often suggestions of workarounds for this in VS Code, but they all have some drawbacks. In my opinion, a much better option is to update the libraries to not use catchError() and instead catch. Having a lint would make it easier to enforce this (and it would be much easier to send PRs to packages that enables and fixes the lint, without fear that they'd accidentally put more .catchError() calls in in the future).

(this was also mentioned at #34144, so there may be other benefits to this too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions