Created
September 27, 2011 15:18
-
-
Save dpickett/1245348 to your computer and use it in GitHub Desktop.
exceptions for flow control is bad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BadController < ApplicationController | |
def create | |
begin | |
some_object.save! | |
rescue Error => e | |
render :json => { :error => e.message }, :status => :unprocessable_entity | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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