Class: RuboCop::Cop::Legion::RescueLogging::SilentCapture

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

Overview

Detects ‘rescue => e` where the captured exception variable is never referenced in the rescue body (not logged or re-raised).

No auto-correct is provided because the fix requires semantic judgment.

Examples:

# bad
rescue => e
  puts 'oops'
end

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

# good
rescue => e
  raise
end

Constant Summary collapse

MSG =
'Exception captured as `%<var>s` but never logged or re-raised. ' \
'Add `log.error(%<var>s.message)` or re-raise.'

Instance Method Summary collapse

Instance Method Details

#on_resbody(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/legion/rescue_logging/silent_capture.rb', line 31

def on_resbody(node)
  return unless node.exception_variable

  var_name = variable_name(node.exception_variable)
  return if var_name.to_s.start_with?('_')

  body = node.body

  return if body && (references_variable?(body, var_name) || contains_raise?(body))

  add_offense(node, message: format(MSG, var: var_name), severity: :warning)
end