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::StatementTimeout` class.
#
# @example
#
# # bad
#
# begin
# run_an_expensive_long_query
# rescue ActiveRecord::StatementTimeout
# try_something_else
# end
#
# @example
#
# # good
#
# run_cheap_queries_with_each_batch
class RescueStatementTimeout < RuboCop::Cop::Base
MSG = <<~EOF
Avoid rescuing the `ActiveRecord::StatementTimeout` 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::StatementTimeout'
end
end
end
end
end
|