diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-12-05 11:24:28 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-12-05 11:24:28 -0600 |
commit | 731dcd84048ef259684cc8005101f65d1941e495 (patch) | |
tree | 02b05367d813fccc777885faa06600a355532ba9 /actionpack/lib/action_controller/rescue.rb | |
parent | 9c9da6c892d715ca22c3cf51f50deb1d80029c66 (diff) | |
download | rails-731dcd84048ef259684cc8005101f65d1941e495.tar.gz rails-731dcd84048ef259684cc8005101f65d1941e495.tar.bz2 rails-731dcd84048ef259684cc8005101f65d1941e495.zip |
Silence server backtrace in rescue templates and log files. Also remove some noise from missing template errors.
Diffstat (limited to 'actionpack/lib/action_controller/rescue.rb')
-rw-r--r-- | actionpack/lib/action_controller/rescue.rb | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/rescue.rb index 9c24c3def4..d7b0e96c93 100644 --- a/actionpack/lib/action_controller/rescue.rb +++ b/actionpack/lib/action_controller/rescue.rb @@ -1,13 +1,19 @@ module ActionController #:nodoc: - # Actions that fail to perform as expected throw exceptions. These exceptions can either be rescued for the public view - # (with a nice user-friendly explanation) or for the developers view (with tons of debugging information). The developers view - # is already implemented by the Action Controller, but the public view should be tailored to your specific application. - # - # The default behavior for public exceptions is to render a static html file with the name of the error code thrown. If no such - # file exists, an empty response is sent with the correct status code. + # Actions that fail to perform as expected throw exceptions. These + # exceptions can either be rescued for the public view (with a nice + # user-friendly explanation) or for the developers view (with tons of + # debugging information). The developers view is already implemented by + # the Action Controller, but the public view should be tailored to your + # specific application. # - # You can override what constitutes a local request by overriding the <tt>local_request?</tt> method in your own controller. - # Custom rescue behavior is achieved by overriding the <tt>rescue_action_in_public</tt> and <tt>rescue_action_locally</tt> methods. + # The default behavior for public exceptions is to render a static html + # file with the name of the error code thrown. If no such file exists, an + # empty response is sent with the correct status code. + # + # You can override what constitutes a local request by overriding the + # <tt>local_request?</tt> method in your own controller. Custom rescue + # behavior is achieved by overriding the <tt>rescue_action_in_public</tt> + # and <tt>rescue_action_locally</tt> methods. module Rescue LOCALHOST = '127.0.0.1'.freeze @@ -32,6 +38,9 @@ module ActionController #:nodoc: 'ActionView::TemplateError' => 'template_error' } + RESCUES_TEMPLATE_PATH = ActionView::PathSet::Path.new( + "#{File.dirname(__FILE__)}/templates", true) + def self.included(base) #:nodoc: base.cattr_accessor :rescue_responses base.rescue_responses = Hash.new(DEFAULT_RESCUE_RESPONSE) @@ -56,12 +65,15 @@ module ActionController #:nodoc: end protected - # Exception handler called when the performance of an action raises an exception. + # Exception handler called when the performance of an action raises + # an exception. def rescue_action(exception) - rescue_with_handler(exception) || rescue_action_without_handler(exception) + rescue_with_handler(exception) || + rescue_action_without_handler(exception) end - # Overwrite to implement custom logging of errors. By default logs as fatal. + # Overwrite to implement custom logging of errors. By default + # logs as fatal. def log_error(exception) #:doc: ActiveSupport::Deprecation.silence do if ActionView::TemplateError === exception @@ -75,16 +87,19 @@ module ActionController #:nodoc: end end - # Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>). By - # default will call render_optional_error_file. Override this method to provide more user friendly error messages. + # Overwrite to implement public exception handling (for requests + # answering false to <tt>local_request?</tt>). By default will call + # render_optional_error_file. Override this method to provide more + # user friendly error messages. def rescue_action_in_public(exception) #:doc: render_optional_error_file response_code_for_rescue(exception) end - - # Attempts to render a static error page based on the <tt>status_code</tt> thrown, - # or just return headers if no such file exists. For example, if a 500 error is - # being handled Rails will first attempt to render the file at <tt>public/500.html</tt>. - # If the file doesn't exist, the body of the response will be left empty. + + # Attempts to render a static error page based on the + # <tt>status_code</tt> thrown, or just return headers if no such file + # exists. For example, if a 500 error is being handled Rails will first + # attempt to render the file at <tt>public/500.html</tt>. If the file + # doesn't exist, the body of the response will be left empty. def render_optional_error_file(status_code) status = interpret_status(status_code) path = "#{Rails.public_path}/#{status[0,3]}.html" @@ -106,11 +121,13 @@ module ActionController #:nodoc: # a controller action. def rescue_action_locally(exception) @template.instance_variable_set("@exception", exception) - @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub"))) - @template.instance_variable_set("@contents", @template.render(:file => template_path_for_local_rescue(exception))) + @template.instance_variable_set("@rescues_path", RESCUES_TEMPLATE_PATH) + @template.instance_variable_set("@contents", + @template.render(:file => template_path_for_local_rescue(exception))) response.content_type = Mime::HTML - render_for_file(rescues_path("layout"), response_code_for_rescue(exception)) + render_for_file(rescues_path("layout"), + response_code_for_rescue(exception)) end def rescue_action_without_handler(exception) @@ -138,7 +155,7 @@ module ActionController #:nodoc: end def rescues_path(template_name) - "#{File.dirname(__FILE__)}/templates/rescues/#{template_name}.erb" + RESCUES_TEMPLATE_PATH["rescues/#{template_name}.erb"] end def template_path_for_local_rescue(exception) @@ -150,8 +167,9 @@ module ActionController #:nodoc: end def clean_backtrace(exception) - defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) ? - Rails.backtrace_cleaner.clean(exception.backtrace) : exception.backtrace + defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) ? + Rails.backtrace_cleaner.clean(exception.backtrace) : + exception.backtrace end end end |