class RuboCop::Cop::Style::RescueStandardError
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: 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
@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
Constants
- MSG_EXPLICIT
- MSG_IMPLICIT
Public Instance Methods
Source
# File lib/rubocop/cop/style/rescue_standard_error.rb, line 92 def on_resbody(node) return if rescue_modifier?(node) case style when :implicit rescue_standard_error?(node) do |error| offense_for_implicit_enforced_style(node, error) end when :explicit rescue_without_error_class?(node) { offense_for_explicit_enforced_style(node) } end end
Private Instance Methods
Source
# File lib/rubocop/cop/style/rescue_standard_error.rb, line 118 def offense_for_explicit_enforced_style(node) add_offense(node.loc.keyword, message: MSG_EXPLICIT) do |corrector| corrector.insert_after(node.loc.keyword, ' StandardError') end end
Source
# File lib/rubocop/cop/style/rescue_standard_error.rb, line 107 def offense_for_implicit_enforced_style(node, error) range = node.loc.keyword.join(error.source_range) add_offense(range, message: MSG_IMPLICIT) do |corrector| error = rescue_standard_error?(node) range = range_between(node.loc.keyword.end_pos, error.source_range.end_pos) corrector.remove(range) end end