Class: RuboCop::Cop::Sidekiq::NoRescueAll

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq/no_rescue_all.rb

Overview

Checks for bare rescue or rescue Exception in Sidekiq jobs.

Rescuing all exceptions can hide bugs and prevent Sidekiq's retry mechanism from working properly. If you need to handle errors, rescue specific exception classes and consider re-raising.

Examples:

# bad
class MyJob
  include Sidekiq::Job

  def perform
    do_work
  rescue
    log_error
  end
end

# bad
class MyJob
  include Sidekiq::Job

  def perform
    do_work
  rescue Exception
    log_error
  end
end

# good
class MyJob
  include Sidekiq::Job

  def perform
    do_work
  rescue NetworkError => e
    log_error(e)
    raise
  end
end

Constant Summary collapse

MSG =
'Avoid rescuing all exceptions in Sidekiq jobs. ' \
'Rescue specific exceptions and consider re-raising.'

Instance Method Summary collapse

Methods included from Sidekiq::Language

#active_job_class?, #perform_call?, #sidekiq_include?, #sidekiq_options_call?

Instance Method Details

#bare_rescue?(node) ⇒ Object



52
53
54
# File 'lib/rubocop/cop/sidekiq/no_rescue_all.rb', line 52

def_node_matcher :bare_rescue?, "(resbody nil? ...)\n"

#on_resbody(node) ⇒ Object



61
62
63
64
65
66
# File 'lib/rubocop/cop/sidekiq/no_rescue_all.rb', line 61

def on_resbody(node)
  return unless in_sidekiq_job?(node)
  return unless bare_rescue?(node) || rescue_exception?(node)

  add_offense(node)
end

#rescue_exception?(node) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/sidekiq/no_rescue_all.rb', line 57

def_node_matcher :rescue_exception?, "(resbody (array (const {nil? cbase} :Exception)) ...)\n"