Class: RuboCop::Cop::Gusto::NoRescueErrorMessageChecking

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/no_rescue_error_message_checking.rb

Overview

Checks for the presence of error message checking within rescue blocks.

This is brittle and can break easily.

NOTE: We submitted this upstream here: github.com/rubocop/rubocop/pull/13352

# bad
begin
  something
rescue => e
  unless e.message.match?(/Duplicate entry/)
    handle_error
  end
end

 # good
 begin
   something
 rescue ActiveRecord::RecordNotUnique => e
   handle_error
 end

Examples:


# bad
begin
  something
rescue => e
  if e.message.match?(/Duplicate entry/)
    handle_error
  end
end

Constant Summary collapse

MSG =
"Avoid checking error message while handling exceptions. This is brittle and can break easily."
METHODS_TO_CHECK =
%i(match? include? ==).to_set.freeze

Instance Method Summary collapse

Instance Method Details

#on_rescue(node) ⇒ Object



43
44
45
46
47
# File 'lib/rubocop/cop/gusto/no_rescue_error_message_checking.rb', line 43

def on_rescue(node)
  node.resbody_branches.last.each_descendant(:if, :unless) do |condition_node|
    add_offense(condition_node) if message_check?(condition_node)
  end
end