Class: Rubocop::Cop::Style::SignalException

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/signal_exception.rb

Overview

This cop checks for uses of fail and raise.

Constant Summary collapse

FAIL_MSG =
'Use `fail` instead of `raise` to signal exceptions.'
RAISE_MSG =
'Use `raise` instead of `fail` to rethrow exceptions.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check_for_fail(node) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/style/signal_exception.rb', line 28

def check_for_fail(node)
  return unless node

  on_node(:send, node, :rescue) do |send_node|
    if command?(:fail, send_node)
      add_offence(:convention, send_node.loc.selector, RAISE_MSG)
    end
  end
end

#check_for_raise(node) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/style/signal_exception.rb', line 18

def check_for_raise(node)
  return unless node

  on_node(:send, node, :rescue) do |send_node|
    if command?(:raise, send_node)
      add_offence(:convention, send_node.loc.selector, FAIL_MSG)
    end
  end
end

#on_rescue(node) ⇒ Object



11
12
13
14
15
16
# File 'lib/rubocop/cop/style/signal_exception.rb', line 11

def on_rescue(node)
  begin_node, rescue_node = *node

  check_for_raise(begin_node)
  check_for_fail(rescue_node)
end