Skip to content

Instantly share code, notes, and snippets.

@dpickett
Created September 27, 2011 15:18
Show Gist options
  • Save dpickett/1245348 to your computer and use it in GitHub Desktop.
Save dpickett/1245348 to your computer and use it in GitHub Desktop.
exceptions for flow control is bad
class BadController < ApplicationController
def create
begin
some_object.save!
rescue Error => e
render :json => { :error => e.message }, :status => :unprocessable_entity
end
end
end
class BestController < ApplicationController
def create
if some_object.save
#do normal rendering logic here
else
render :json => { :error => e.message }, :status => :unprocessable_entity
end
end
end
class BetterController < ApplicationController
def create
begin
some_object.save!
rescue ActiveRecord::RecordInvalid => e
render :json => { :error => e.message }, :status => :unprocessable_entity
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment