Skip to content

DIscourage rescue error message checking #951

@simulk

Description

@simulk

I proposed PR rubocop/rubocop#13352 to add a new Lint/NoRescueErrorMessageChecking cop that discourages rescue error matching. We built a custom cop at my company and thought it might be useful to roll it out as built in cop.

I was suggested by @dvandersluis to reach out to the community for feedback whether its a useful cop or not.

# bad
begin
  something
rescue => e
  if e.message.match?(/Duplicate entry/)
    handle_error
  end
end

# bad
begin
  something
rescue => e
  unless e.message.match?(/Duplicate entry/)
    handle_error
  end
end

# good
begin
  something
rescue ActiveRecord::RecordNotUnique => e
  handle_error
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions