Created
April 5, 2013 11:07
-
-
Save mfo/5318501 to your computer and use it in GitHub Desktop.
Looking for an idea for a graceful way to DRY repetition regarding the error rescuing...
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 Api::CollectionsController < ApplicationController | |
def update | |
@collection = Collection.where(pname: params[:id], event_id: @event.id).first | |
@collection.assign_to(@event, params[:collection]) | |
@status = @collection.save! ? "ok" : "ko" | |
render status: 200 | |
rescue Exception => e | |
@status = "ArgumentError" | |
@error = @collection.errors.keys | |
render status: 400 | |
end | |
def create | |
@collection = Collection.new_by_type(params[:collection][:type]) | |
@collection.assign_to(@event, params[:collection]) | |
@status = @collection.save! ? "ok" : "ko" | |
render status: 200 | |
rescue NameError => e | |
@status = "NameError" | |
@error = ["type"] | |
render status: 400 | |
rescue Exception => e | |
@status = "ArgumentError" | |
@error = @collection.errors.keys | |
render status: 400 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx to @JohnMeiss for the link : http://stackoverflow.com/questions/8644577/how-to-rescue-exception-central-and-dry