Class: RuboCop::Cop::Legion::RescueLogging::NoCapture

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/legion/rescue_logging/no_capture.rb

Overview

Detects ‘rescue SomeError` where the exception class is specified but no `=> e` capture is present, and auto-corrects by appending `=> e`.

Examples:

# bad
rescue StandardError
  nil
end

# bad
rescue ArgumentError, TypeError
  nil
end

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

Constant Summary collapse

MSG =
'Exception class specified but not captured. ' \
'Use `rescue %<classes>s => e` and log the exception.'

Instance Method Summary collapse

Instance Method Details

#on_resbody(node) ⇒ Object



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

def on_resbody(node)
  return if node.exceptions.empty? || !node.exception_variable.nil?
  return if rescue_modifier?(node)

  classes = node.exceptions.map(&:source).join(', ')
  message = format(MSG, classes: classes)

  add_offense(node, message: message, severity: :convention)
end