class RuboCop::Cop::Style::RescueStandardError

This cop checks for rescuing `StandardError`. There are two supported styles `implicit` and `explicit`. This cop will not register an offense if any error other than `StandardError` is specified.

@example EnforcedStyle: implicit

# `implicit` will enforce using `rescue` instead of
# `rescue StandardError`.

# bad
begin
  foo
rescue StandardError
  bar
end

# good
begin
  foo
rescue
  bar
end

# good
begin
  foo
rescue OtherError
  bar
end

# good
begin
  foo
rescue StandardError, SecurityError
  bar
end

@example EnforcedStyle: explicit (default)

# `explicit` will enforce using `rescue StandardError`
# instead of `rescue`.

# bad
begin
  foo
rescue
  bar
end

# good
begin
  foo
rescue StandardError
  bar
end

# good
begin
  foo
rescue OtherError
  bar
end

# good
begin
  foo
rescue StandardError, SecurityError
  bar
end

Constants

MSG_EXPLICIT
MSG_IMPLICIT

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/rescue_standard_error.rb, line 108
def autocorrect(node)
  lambda do |corrector|
    case style
    when :implicit
      error = rescue_standard_error?(node)
      range = range_between(node.loc.keyword.end_pos,
                            error.loc.expression.end_pos)
      corrector.remove(range)
    when :explicit
      corrector.insert_after(node.loc.keyword, ' StandardError')
    end
  end
end
on_resbody(node) click to toggle source
# File lib/rubocop/cop/style/rescue_standard_error.rb, line 91
def on_resbody(node)
  return if rescue_modifier?(node)

  case style
  when :implicit
    rescue_standard_error?(node) do |error|
      add_offense(node,
                  location: node.loc.keyword.join(error.loc.expression),
                  message: MSG_IMPLICIT)
    end
  when :explicit
    rescue_without_error_class?(node) do
      add_offense(node, location: :keyword, message: MSG_EXPLICIT)
    end
  end
end