Search code examples
ruby-on-railsrubocop

Provide an exception class and message as arguments to raise in Rails


I'm doing some code clean up and I keep getting this offense in Rubocop. It applies to this section:

def load_user
 @user = OtherUser.friendly.find(params[:id])
 raise Other::NotFoundError.new('user') if @user.blank?
end

I thought I could simply put in a rescue ArgumentError above the raise but that did not resolve it. How do I address the exception class?

Edit: Changing it to

raise Other::NotFoundError, 'user' ? if @user.blank?

Results in unexpected token kDEF on the next line and then unexpected token $end for the end.


Solution

  • Your revision contains an unnecessary '?'.

    Try this:

    raise Other::NotFoundError, 'user' if @user.blank?