Class: RuboCop::Cop::Legion::RescueLogging::BareRescue

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/rescue_logging/bare_rescue.rb

Overview

Detects bare ‘rescue` with no exception class and no variable capture, and auto-corrects by adding `=> e`.

Examples:

# bad
begin
  risky
rescue
  nil
end

# good
begin
  risky
rescue => e
  log.error(e.message)
end

Constant Summary collapse

MSG =
'Bare `rescue` swallows all StandardError silently. ' \
'Capture the exception with `rescue => e` and log it.'

Instance Method Summary collapse

Instance Method Details

#on_resbody(node) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/legion/rescue_logging/bare_rescue.rb', line 30

def on_resbody(node)
  return unless node.exceptions.empty? && node.exception_variable.nil?
  return if rescue_modifier?(node)

  add_offense(node, severity: :warning) do |corrector|
    corrector.insert_after(node.loc.keyword, ' => e')
  end
end