Sha256: fa42405c51b6130f56ab8c30aa54d85ac5fff6e732a66b242bb80b792a4f70ac

Contents?: true

Size: 1 KB

Versions: 49

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for *rescue* blocks targeting the Exception class.
      #
      # @example
      #
      #   # bad
      #
      #   begin
      #     do_something
      #   rescue Exception
      #     handle_exception
      #   end
      #
      # @example
      #
      #   # good
      #
      #   begin
      #     do_something
      #   rescue ArgumentError
      #     handle_exception
      #   end
      class RescueException < Cop
        MSG = 'Avoid rescuing the `Exception` class. ' \
              'Perhaps you meant to rescue `StandardError`?'

        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 == 'Exception'
        end
      end
    end
  end
end

Version data entries

49 entries across 30 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.84.0 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.83.0 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.82.0 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.81.0 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.80.1 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.80.0 lib/rubocop/cop/lint/rescue_exception.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/rescue_exception.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.79.0 lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.78.0 lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/rescue_exception.rb
rubocop-0.77.0 lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/lint/rescue_exception.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/lint/rescue_exception.rb