Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_controller/rescue.rb

Revision 8365, 10.4 kB (checked in by bitsweat, 1 year ago)

Ruby 1.9 compat: File.exists\? -> File.exist\? en masse. References #1689 [Pratik Naik]

Line 
1 module ActionController #:nodoc:
2   # Actions that fail to perform as expected throw exceptions. These exceptions can either be rescued for the public view
3   # (with a nice user-friendly explanation) or for the developers view (with tons of debugging information). The developers view
4   # is already implemented by the Action Controller, but the public view should be tailored to your specific application.
5   #
6   # The default behavior for public exceptions is to render a static html file with the name of the error code thrown.  If no such
7   # file exists, an empty response is sent with the correct status code.
8   #
9   # You can override what constitutes a local request by overriding the <tt>local_request?</tt> method in your own controller.
10   # Custom rescue behavior is achieved by overriding the <tt>rescue_action_in_public</tt> and <tt>rescue_action_locally</tt> methods.
11   module Rescue
12     LOCALHOST = '127.0.0.1'.freeze
13
14     DEFAULT_RESCUE_RESPONSE = :internal_server_error
15     DEFAULT_RESCUE_RESPONSES = {
16       'ActionController::RoutingError'             => :not_found,
17       'ActionController::UnknownAction'            => :not_found,
18       'ActiveRecord::RecordNotFound'               => :not_found,
19       'ActiveRecord::StaleObjectError'             => :conflict,
20       'ActiveRecord::RecordInvalid'                => :unprocessable_entity,
21       'ActiveRecord::RecordNotSaved'               => :unprocessable_entity,
22       'ActionController::MethodNotAllowed'         => :method_not_allowed,
23       'ActionController::NotImplemented'           => :not_implemented,
24       'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
25     }
26
27     DEFAULT_RESCUE_TEMPLATE = 'diagnostics'
28     DEFAULT_RESCUE_TEMPLATES = {
29       'ActionController::MissingTemplate' => 'missing_template',
30       'ActionController::RoutingError'    => 'routing_error',
31       'ActionController::UnknownAction'   => 'unknown_action',
32       'ActionView::TemplateError'         => 'template_error'
33     }
34
35     def self.included(base) #:nodoc:
36       base.cattr_accessor :rescue_responses
37       base.rescue_responses = Hash.new(DEFAULT_RESCUE_RESPONSE)
38       base.rescue_responses.update DEFAULT_RESCUE_RESPONSES
39
40       base.cattr_accessor :rescue_templates
41       base.rescue_templates = Hash.new(DEFAULT_RESCUE_TEMPLATE)
42       base.rescue_templates.update DEFAULT_RESCUE_TEMPLATES
43
44       base.class_inheritable_array :rescue_handlers
45       base.rescue_handlers = []
46
47       base.extend(ClassMethods)
48       base.class_eval do
49         alias_method_chain :perform_action, :rescue
50       end
51     end
52
53     module ClassMethods
54       def process_with_exception(request, response, exception) #:nodoc:
55         new.process(request, response, :rescue_action, exception)
56       end
57
58       # Rescue exceptions raised in controller actions.
59       #
60       # <tt>rescue_from</tt> receives a series of exception classes or class
61       # names, and a trailing :with option with the name of a method or a Proc
62       # object to be called to handle them. Alternatively a block can be given.
63       #
64       # Handlers that take one argument will be called with the exception, so
65       # that the exception can be inspected when dealing with it.
66       #
67       # Handlers are inherited. They are searched from right to left, from
68       # bottom to top, and up the hierarchy. The handler of the first class for
69       # which exception.is_a?(klass) holds true is the one invoked, if any.
70       #
71       # class ApplicationController < ActionController::Base
72       #   rescue_from User::NotAuthorized, :with => :deny_access # self defined exception
73       #   rescue_from ActiveRecord::RecordInvalid, :with => :show_errors
74       #
75       #   rescue_from 'MyAppError::Base' do |exception|
76       #     render :xml => exception, :status => 500
77       #   end
78       #
79       #   protected
80       #     def deny_access
81       #       ...
82       #     end
83       #
84       #     def show_errors(exception)
85       #       exception.record.new_record? ? ...
86       #     end
87       # end
88       def rescue_from(*klasses, &block)
89         options = klasses.extract_options!
90         unless options.has_key?(:with)
91           block_given? ? options[:with] = block : raise(ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument.")
92         end
93
94         klasses.each do |klass|
95           key = if klass.is_a?(Class) && klass <= Exception
96             klass.name
97           elsif klass.is_a?(String)
98             klass
99           else
100             raise(ArgumentError, "#{klass} is neither an Exception nor a String")
101           end
102
103           # Order is important, we put the pair at the end. When dealing with an
104           # exception we will follow the documented order going from right to left.
105           rescue_handlers << [key, options[:with]]
106         end
107       end
108     end
109
110     protected
111       # Exception handler called when the performance of an action raises an exception.
112       def rescue_action(exception)
113         log_error(exception) if logger
114         erase_results if performed?
115
116         # Let the exception alter the response if it wants.
117         # For example, MethodNotAllowed sets the Allow header.
118         if exception.respond_to?(:handle_response!)
119           exception.handle_response!(response)
120         end
121
122         if consider_all_requests_local || local_request?
123           rescue_action_locally(exception)
124         else
125           rescue_action_in_public(exception)
126         end
127       end
128
129       # Overwrite to implement custom logging of errors. By default logs as fatal.
130       def log_error(exception) #:doc:
131         ActiveSupport::Deprecation.silence do
132           if ActionView::TemplateError === exception
133             logger.fatal(exception.to_s)
134           else
135             logger.fatal(
136               "\n\n#{exception.class} (#{exception.message}):\n    " +
137               clean_backtrace(exception).join("\n    ") +
138               "\n\n"
139             )
140           end
141         end
142       end
143
144       # Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>).  By
145       # default will call render_optional_error_file.  Override this method to provide more user friendly error messages.s
146       def rescue_action_in_public(exception) #:doc:
147         render_optional_error_file response_code_for_rescue(exception)
148       end
149      
150       # Attempts to render a static error page based on the <tt>status_code</tt> thrown,
151       # or just return headers if no such file exists. For example, if a 500 error is
152       # being handled Rails will first attempt to render the file at <tt>public/500.html</tt>.
153       # If the file doesn't exist, the body of the response will be left empty.
154       def render_optional_error_file(status_code)
155         status = interpret_status(status_code)
156         path = "#{RAILS_ROOT}/public/#{status[0,3]}.html"
157         if File.exist?(path)
158           render :file => path, :status => status
159         else
160           head status
161         end
162       end
163
164       # True if the request came from localhost, 127.0.0.1. Override this
165       # method if you wish to redefine the meaning of a local request to
166       # include remote IP addresses or other criteria.
167       def local_request? #:doc:
168         request.remote_addr == LOCALHOST and request.remote_ip == LOCALHOST
169       end
170
171       # Render detailed diagnostics for unhandled exceptions rescued from
172       # a controller action.
173       def rescue_action_locally(exception)
174         add_variables_to_assigns
175         @template.instance_variable_set("@exception", exception)
176         @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub")))
177         @template.send!(:assign_variables_from_controller)
178
179         @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false))
180
181         response.content_type = Mime::HTML
182         render_for_file(rescues_path("layout"), response_code_for_rescue(exception))
183       end
184
185       # Tries to rescue the exception by looking up and calling a registered handler.
186       def rescue_action_with_handler(exception)
187         if handler = handler_for_rescue(exception)
188           if handler.arity != 0
189             handler.call(exception)
190           else
191             handler.call
192           end
193           true # don't rely on the return value of the handler
194         end
195       end
196
197     private
198       def perform_action_with_rescue #:nodoc:
199         perform_action_without_rescue
200       rescue Exception => exception  # errors from action performed
201         return if rescue_action_with_handler(exception)
202        
203         rescue_action(exception)
204       end
205
206       def rescues_path(template_name)
207         "#{File.dirname(__FILE__)}/templates/rescues/#{template_name}.erb"
208       end
209
210       def template_path_for_local_rescue(exception)
211         rescues_path(rescue_templates[exception.class.name])
212       end
213
214       def response_code_for_rescue(exception)
215         rescue_responses[exception.class.name]
216       end
217
218       def handler_for_rescue(exception)
219         # We go from right to left because pairs are pushed onto rescue_handlers
220         # as rescue_from declarations are found.
221         _, handler = *rescue_handlers.reverse.detect do |klass_name, handler|
222           # The purpose of allowing strings in rescue_from is to support the
223           # declaration of handler associations for exception classes whose
224           # definition is yet unknown.
225           #
226           # Since this loop needs the constants it would be inconsistent to
227           # assume they should exist at this point. An early raised exception
228           # could trigger some other handler and the array could include
229           # precisely a string whose corresponding constant has not yet been
230           # seen. This is why we are tolerant to unknown constants.
231           #
232           # Note that this tolerance only matters if the exception was given as
233           # a string, otherwise a NameError will be raised by the interpreter
234           # itself when rescue_from CONSTANT is executed.
235           klass = self.class.const_get(klass_name) rescue nil
236           klass ||= klass_name.constantize rescue nil
237           exception.is_a?(klass) if klass
238         end
239
240         case handler
241         when Symbol
242           method(handler)
243         when Proc
244           handler.bind(self)
245         end
246       end
247
248       def clean_backtrace(exception)
249         if backtrace = exception.backtrace
250           if defined?(RAILS_ROOT)
251             backtrace.map { |line| line.sub RAILS_ROOT, '' }
252           else
253             backtrace
254           end
255         end
256       end
257   end
258 end
Note: See TracBrowser for help on using the browser.