Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rescue_query_canceled.rb « database « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1238f9ed911dc9f728cbecdd5cf661c229ff71a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module RuboCop
  module Cop
    module Database
      # Checks for `rescue` blocks targeting the `ActiveRecord::QueryCanceled` class.
      #
      # @example
      #
      #   # bad
      #
      #   begin
      #     run_an_expensive_long_query
      #   rescue ActiveRecord::QueryCanceled
      #     try_something_else
      #   end
      #
      # @example
      #
      #   # good
      #
      #   run_cheap_queries_with_each_batch
      class RescueQueryCanceled < RuboCop::Cop::Cop
        MSG = <<~EOF
          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`.
        EOF

        def on_resbody(node)
          return unless node.children.first

          rescue_args = node.children.first.children
          return unless rescue_args.any? { |a| targets_exception?(a) }

          add_offense(node)
        end

        def targets_exception?(rescue_arg_node)
          rescue_arg_node.const_name == 'ActiveRecord::QueryCanceled'
        end
      end
    end
  end
end