1 module ActionController
#:nodoc:
2 # Actions that fail to perform as expected throw exceptions. These
3 # exceptions can either be rescued for the public view (with a nice
4 # user-friendly explanation) or for the developers view (with tons of
5 # debugging information). The developers view is already implemented by
6 # the Action Controller, but the public view should be tailored to your
7 # specific application.
9 # The default behavior for public exceptions is to render a static html
10 # file with the name of the error code thrown. If no such file exists, an
11 # empty response is sent with the correct status code.
13 # You can override what constitutes a local request by overriding the
14 # <tt>local_request?</tt> method in your own controller. Custom rescue
15 # behavior is achieved by overriding the <tt>rescue_action_in_public</tt>
16 # and <tt>rescue_action_locally</tt> methods.
18 LOCALHOST
= '127.0.0.1'.freeze
20 DEFAULT_RESCUE_RESPONSE
= :internal_server_error
21 DEFAULT_RESCUE_RESPONSES
= {
22 'ActionController::RoutingError' => :not_found,
23 'ActionController::UnknownAction' => :not_found,
24 'ActiveRecord::RecordNotFound' => :not_found,
25 'ActiveRecord::StaleObjectError' => :conflict,
26 'ActiveRecord::RecordInvalid' => :unprocessable_entity,
27 'ActiveRecord::RecordNotSaved' => :unprocessable_entity,
28 'ActionController::MethodNotAllowed' => :method_not_allowed,
29 'ActionController::NotImplemented' => :not_implemented,
30 'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
33 DEFAULT_RESCUE_TEMPLATE
= 'diagnostics'
34 DEFAULT_RESCUE_TEMPLATES
= {
35 'ActionView::MissingTemplate' => 'missing_template',
36 'ActionController::RoutingError' => 'routing_error',
37 'ActionController::UnknownAction' => 'unknown_action',
38 'ActionView::TemplateError' => 'template_error'
41 RESCUES_TEMPLATE_PATH
= ActionView
::Template::EagerPath.new_and_loaded(
42 File
.join(File
.dirname(__FILE__
), "templates"))
44 def self.included(base
) #:nodoc:
45 base
.cattr_accessor
:rescue_responses
46 base
.rescue_responses
= Hash
.new(DEFAULT_RESCUE_RESPONSE
)
47 base
.rescue_responses
.update DEFAULT_RESCUE_RESPONSES
49 base
.cattr_accessor
:rescue_templates
50 base
.rescue_templates
= Hash
.new(DEFAULT_RESCUE_TEMPLATE
)
51 base
.rescue_templates
.update DEFAULT_RESCUE_TEMPLATES
53 base
.extend(ClassMethods
)
54 base
.send
:include, ActiveSupport
::Rescuable
57 alias_method_chain
:perform_action, :rescue
62 def call_with_exception(env, exception
) #:nodoc:
63 request
= env["action_controller.rescue.request"] ||= Request
.new(env)
64 response
= env["action_controller.rescue.response"] ||= Response
.new
65 new
.process(request
, response
, :rescue_action, exception
)
70 # Exception handler called when the performance of an action raises
72 def rescue_action(exception
)
73 rescue_with_handler(exception
) ||
74 rescue_action_without_handler(exception
)
77 # Overwrite to implement custom logging of errors. By default
79 def log_error(exception
) #:doc:
80 ActiveSupport
::Deprecation.silence
do
81 if ActionView
::TemplateError === exception
82 logger
.fatal(exception
.to_s
)
85 "\n#{exception.class} (#{exception.message}):\n " +
86 clean_backtrace(exception
).join("\n ") + "\n\n"
92 # Overwrite to implement public exception handling (for requests
93 # answering false to <tt>local_request?</tt>). By default will call
94 # render_optional_error_file. Override this method to provide more
95 # user friendly error messages.
96 def rescue_action_in_public(exception
) #:doc:
97 render_optional_error_file
response_code_for_rescue(exception
)
100 # Attempts to render a static error page based on the
101 # <tt>status_code</tt> thrown, or just return headers if no such file
102 # exists. At first, it will try to render a localized static page.
103 # For example, if a 500 error is being handled Rails and locale is :da,
104 # it will first attempt to render the file at <tt>public/500.da.html</tt>
105 # then attempt to render <tt>public/500.html</tt>. If none of them exist,
106 # the body of the response will be left empty.
107 def render_optional_error_file(status_code
)
108 status
= interpret_status(status_code
)
109 locale_path
= "#{Rails.public_path}/#{status[0,3]}.#{I18n.locale}.html" if I18n
.locale
110 path
= "#{Rails.public_path}/#{status[0,3]}.html"
112 if locale_path
&& File
.exist
?(locale_path
)
113 render
:file => locale_path
, :status => status
, :content_type => Mime
::HTML
114 elsif File
.exist
?(path
)
115 render
:file => path
, :status => status
, :content_type => Mime
::HTML
121 # True if the request came from localhost, 127.0.0.1. Override this
122 # method if you wish to redefine the meaning of a local request to
123 # include remote IP addresses or other criteria.
124 def local_request
? #:doc:
125 request
.remote_addr
== LOCALHOST
&& request
.remote_ip
== LOCALHOST
128 # Render detailed diagnostics for unhandled exceptions rescued from
129 # a controller action.
130 def rescue_action_locally(exception
)
131 @template.instance_variable_set("@exception", exception
)
132 @template.instance_variable_set("@rescues_path", RESCUES_TEMPLATE_PATH
)
133 @template.instance_variable_set("@contents",
134 @template.render(:file => template_path_for_local_rescue(exception
)))
136 response
.content_type
= Mime
::HTML
137 render_for_file(rescues_path("layout"),
138 response_code_for_rescue(exception
))
141 def rescue_action_without_handler(exception
)
142 log_error(exception
) if logger
143 erase_results
if performed
?
145 # Let the exception alter the response if it wants.
146 # For example, MethodNotAllowed sets the Allow header.
147 if exception
.respond_to
?(:handle_response!)
148 exception
.handle_response
!(response
)
151 if consider_all_requests_local
|| local_request
?
152 rescue_action_locally(exception
)
154 rescue_action_in_public(exception
)
159 def perform_action_with_rescue
#:nodoc:
160 perform_action_without_rescue
161 rescue Exception
=> exception
162 rescue_action(exception
)
165 def rescues_path(template_name
)
166 RESCUES_TEMPLATE_PATH
["rescues/#{template_name}.erb"]
169 def template_path_for_local_rescue(exception
)
170 rescues_path(rescue_templates
[exception
.class.name
])
173 def response_code_for_rescue(exception
)
174 rescue_responses
[exception
.class.name
]
177 def clean_backtrace(exception
)
178 defined?(Rails
) && Rails
.respond_to
?(:backtrace_cleaner) ?
179 Rails
.backtrace_cleaner
.clean(exception
.backtrace
) :