Module | ActionController::Rescue |
In: |
vendor/rails/actionpack/lib/action_controller/rescue.rb
|
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. So too could the decision on whether something is a public or a developer request.
You can tailor the rescuing behavior and appearance by overwriting the following two stub methods.
Overwrite to expand the meaning of a local request in order to show local rescues on other occurrences than the remote IP being 127.0.0.1. For example, this could include the IP of the developer machine when debugging remotely.
# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 61 61: def local_request? #:doc: 62: [@request.remote_addr, @request.remote_ip] == ["127.0.0.1"] * 2 63: end
Overwrite to implement custom logging of errors. By default logs as fatal.
# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 36 36: def log_error(exception) #:doc: 37: if ActionView::TemplateError === exception 38: logger.fatal(exception.to_s) 39: else 40: logger.fatal( 41: "\n\n#{exception.class} (#{exception.message}):\n " + 42: clean_backtrace(exception).join("\n ") + 43: "\n\n" 44: ) 45: end 46: end
Exception handler called when the performance of an action raises an exception.
# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 24 24: def rescue_action(exception) 25: log_error(exception) if logger 26: erase_results if performed? 27: 28: if consider_all_requests_local || local_request? 29: rescue_action_locally(exception) 30: else 31: rescue_action_in_public(exception) 32: end 33: end
Overwrite to implement public exception handling (for requests answering false to local_request?).
# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 49 49: def rescue_action_in_public(exception) #:doc: 50: case exception 51: when RoutingError, UnknownAction 52: render_text(IO.read(File.join(RAILS_ROOT, 'public', '404.html')), "404 Not Found") 53: else 54: render_text(IO.read(File.join(RAILS_ROOT, 'public', '500.html')), "500 Internal Error") 55: end 56: end
Renders a detailed diagnostics screen on action exceptions.
# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 66 66: def rescue_action_locally(exception) 67: add_variables_to_assigns 68: @template.instance_variable_set("@exception", exception) 69: @template.instance_variable_set("@rescues_path", File.dirname(__FILE__) + "/templates/rescues/") 70: @template.send(:assign_variables_from_controller) 71: 72: @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false)) 73: 74: @headers["Content-Type"] = "text/html" 75: render_file(rescues_path("layout"), response_code_for_rescue(exception)) 76: end