Skip to content

Instantly share code, notes, and snippets.

@clayenga
Created October 9, 2015 17:45
Show Gist options
  • Save clayenga/ad2cab9341d347196c43 to your computer and use it in GitHub Desktop.
Save clayenga/ad2cab9341d347196c43 to your computer and use it in GitHub Desktop.
Error handling comparisons
def SomeController < ApplicationController
def some_action_1
line_item = LineItem.find(params[:line_item_id])
user_qty = Integer(params[:user_qty]) rescue nil
unless user_qty && (1...line_item.quantity) === user_qty
render partial: '/my/error/partial'
return
end
# do some stuff
render partial: '/my/main/partial'
end
def some_action_2
line_item = LineItem.find(params[:line_item_id])
user_qty = Integer(params[:user_qty])
raise ArgumentError unless (1...line_item.quantity) === user_qty
# do some stuff
render partial: '/my/main/partial'
rescue ArgumentError
render partial: '/my/error/partial'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment