Skip to content

Add Rubocop to discourage catching `QueryCanceled` or `StatementTimeout` exceptions

What does this MR do and why?

Adds a Rubocop cop to discourage catching QueryCanceled or StatementTimeout exceptions as this pattern should be rarely used or only for temporary patches. We should instead encourage refactoring to use less expensive queries with helpers such as each_batch, rather than running single expensive queries and falling back to do something else when they fail.

How to set up and validate locally

  1. Add a rescue ActiveRecord::QueryCanceled => e
  2. Run rubocop
  3. See an offense:
    app/services/test_service.rb:1:7: C: Database/RescueQueryCanceled: Avoid rescuing the ActiveRecord::QueryCanceled class.
    
    Using this pattern should be a very rare exception or a temporary patch only.
    Consider refactoring using less expensive queries and each_batch.
    
          rescue ActiveRecord::QueryCanceled => err ...
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #245327 (closed)

Edited by Matt Kasa

Merge request reports