Skip to content

New lint suggestion: uncaught_async_error_in_try_catch #59364

@gmpassos

Description

@gmpassos

I suggest the lint uncaught_async_error_in_try_catch (or a better name), for the example bellow:

/// Here the call to [_computeImpl] should trigger a warning,
/// since the `try` block won't catch any error from `_computeImpl`:
Future<int?> compute1(String value) async {
  try {
    return _computeImpl(value) ;
  } catch(_) {
    return null ;
  }
}

/// This is the fixed version, where the `await` ensures
/// that the `try` block will catch errors.
Future<int?> compute2(String value) async {
  try {
    return await _computeImpl(value) ;
  } catch(_) {
    return null ;
  }
}

/// A simple computation that can trigger an [ArgumentError].
Future<int?> _computeImpl(String value) async {
  if (value.isEmpty) {
    throw ArgumentError("Empty `value`");
  }

  return value.length ;
}

Without a lint the issue above is not simple to detect and debug and can generate many bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions