diff options
author | Ryan Bigg <radarlistener@gmail.com> | 2008-10-18 17:43:38 +1030 |
---|---|---|
committer | Ryan Bigg <radarlistener@gmail.com> | 2008-10-18 17:43:38 +1030 |
commit | 2139a1b6812be7ca86de2df52e9776a2be4a2bf7 (patch) | |
tree | 3131e29d3582b8aa1ba1c68710f327ce2f22aa1d /railties/doc/guides/actioncontroller/rescue.txt | |
parent | 09b7e351316cb87a815678241fc90af549327cf3 (diff) | |
parent | 095cafbcd7fbae3baa845b23b93c8dca93b442f8 (diff) | |
download | rails-2139a1b6812be7ca86de2df52e9776a2be4a2bf7.tar.gz rails-2139a1b6812be7ca86de2df52e9776a2be4a2bf7.tar.bz2 rails-2139a1b6812be7ca86de2df52e9776a2be4a2bf7.zip |
Merge branch 'master' of git@github.com:lifo/docrails
Diffstat (limited to 'railties/doc/guides/actioncontroller/rescue.txt')
-rw-r--r-- | railties/doc/guides/actioncontroller/rescue.txt | 53 |
1 files changed, 1 insertions, 52 deletions
diff --git a/railties/doc/guides/actioncontroller/rescue.txt b/railties/doc/guides/actioncontroller/rescue.txt index 50583bb71e..ec03006764 100644 --- a/railties/doc/guides/actioncontroller/rescue.txt +++ b/railties/doc/guides/actioncontroller/rescue.txt @@ -64,55 +64,4 @@ private end ----------------------------------- -NOTE: Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Partik Naik's link:http://m.onkey.org/2008/7/20/rescue-from-dispatching[article] on the subject for more information. - -=== `rescue_action` === - -The `rescue_from` method was added to make it easier to rescue different kinds of exceptions and deal with each separately. Action Controller has a default method which intercepts *all* exceptions raised, `rescue_action`. You can override this method in a controller or in ApplicationController to rescue all exceptions raised in that particular context. You can get a little bit more granular by using the link:http://api.rubyonrails.org/classes/ActionController/Rescue.html#M000615[rescue_action_in_public] and link:http://api.rubyonrails.org/classes/ActionController/Rescue.html#M000618[rescue_action_locally] methods which are used to rescue actions for public and local requests. Let's see how the User::NotAuthorized exception could be caught using this technique: - -[source, ruby] ----------------------------------------- -class ApplicationController < ActionController::Base - -private - - def rescue_action_in_public(exception) - case exception - when User::NotAuthorized - user_not_authorized - else - super - end - end - -end ----------------------------------------- - -As you can see, this gets a bit messy once you start rescuing various types of error that require separate handlers, so it's a good idea to use `rescue_from` instead. - -=== Getting down and dirty === - -Of course you can still use Ruby's `rescue` to rescue exceptions wherever you want. This is usually constrained to single methods, i.e. actions, but is still a very useful technique that should be used when appropriate. For example, you might use an API that raises a timeout error in one of your actions, and you have to handle that if it's raised: - -[source, ruby] ----------------------------------------- -require 'clients_international' -class ClientsController < ApplicationController - - def update - @client = Client.find(params[:id]) - @client.attributes = params[:client] - if @client.save - flash[:notice] = "Client was updated" - ClientsInternational.send_update(@client.to_xml) - redirect_to clients_url - else - render :action => "new" - end - rescue ClientsInternational::TimeoutError - flash[:error] = "Couldn't send API update" - redirect_to @client - end - -end ----------------------------------------- +NOTE: Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's link:http://m.onkey.org/2008/7/20/rescue-from-dispatching[article] on the subject for more information. |