rubocop / ruby-style-guide

A community-driven Ruby coding style guide
https://rubystyle.guide
16.47k stars 3.4k forks source link

DIscourage rescue error message checking #951

Open simulk opened 1 month ago

simulk commented 1 month ago

I proposed PR https://github.com/rubocop/rubocop/pull/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
pirj commented 1 month ago

As a cop - yes.

As a guideline - too rare and I’d say not worth adding.

If you’re courageous enough, harvest real-world-ruby-apps and real-world-rails repos for usage examples.

I’m more inclined to close.