Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 767
Jake Avatar asked Oct 16 '25 22:10

Jake


1 Answers

Your revision contains an unnecessary '?'.

Try this:

raise Other::NotFoundError, 'user' if @user.blank?
like image 141
Andy Waite Avatar answered Oct 18 '25 14:10

Andy Waite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!