Froze rails gems
[depot.git] / vendor / rails / actionpack / lib / action_controller / rescue.rb
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 'ActionView::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.extend(ClassMethods)
45 base.send :include, ActiveSupport::Rescuable
46
47 base.class_eval do
48 alias_method_chain :perform_action, :rescue
49 end
50 end
51
52 module ClassMethods
53 def process_with_exception(request, response, exception) #:nodoc:
54 new.process(request, response, :rescue_action, exception)
55 end
56 end
57
58 protected
59 # Exception handler called when the performance of an action raises an exception.
60 def rescue_action(exception)
61 rescue_with_handler(exception) || rescue_action_without_handler(exception)
62 end
63
64 # Overwrite to implement custom logging of errors. By default logs as fatal.
65 def log_error(exception) #:doc:
66 ActiveSupport::Deprecation.silence do
67 if ActionView::TemplateError === exception
68 logger.fatal(exception.to_s)
69 else
70 logger.fatal(
71 "\n\n#{exception.class} (#{exception.message}):\n " +
72 clean_backtrace(exception).join("\n ") +
73 "\n\n"
74 )
75 end
76 end
77 end
78
79 # Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>). By
80 # default will call render_optional_error_file. Override this method to provide more user friendly error messages.
81 def rescue_action_in_public(exception) #:doc:
82 render_optional_error_file response_code_for_rescue(exception)
83 end
84
85 # Attempts to render a static error page based on the <tt>status_code</tt> thrown,
86 # or just return headers if no such file exists. For example, if a 500 error is
87 # being handled Rails will first attempt to render the file at <tt>public/500.html</tt>.
88 # If the file doesn't exist, the body of the response will be left empty.
89 def render_optional_error_file(status_code)
90 status = interpret_status(status_code)
91 path = "#{Rails.public_path}/#{status[0,3]}.html"
92 if File.exist?(path)
93 render :file => path, :status => status
94 else
95 head status
96 end
97 end
98
99 # True if the request came from localhost, 127.0.0.1. Override this
100 # method if you wish to redefine the meaning of a local request to
101 # include remote IP addresses or other criteria.
102 def local_request? #:doc:
103 request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST
104 end
105
106 # Render detailed diagnostics for unhandled exceptions rescued from
107 # a controller action.
108 def rescue_action_locally(exception)
109 @template.instance_variable_set("@exception", exception)
110 @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub")))
111 @template.instance_variable_set("@contents", @template.render(:file => template_path_for_local_rescue(exception)))
112
113 response.content_type = Mime::HTML
114 render_for_file(rescues_path("layout"), response_code_for_rescue(exception))
115 end
116
117 def rescue_action_without_handler(exception)
118 log_error(exception) if logger
119 erase_results if performed?
120
121 # Let the exception alter the response if it wants.
122 # For example, MethodNotAllowed sets the Allow header.
123 if exception.respond_to?(:handle_response!)
124 exception.handle_response!(response)
125 end
126
127 if consider_all_requests_local || local_request?
128 rescue_action_locally(exception)
129 else
130 rescue_action_in_public(exception)
131 end
132 end
133
134 private
135 def perform_action_with_rescue #:nodoc:
136 perform_action_without_rescue
137 rescue Exception => exception
138 rescue_action(exception)
139 end
140
141 def rescues_path(template_name)
142 "#{File.dirname(__FILE__)}/templates/rescues/#{template_name}.erb"
143 end
144
145 def template_path_for_local_rescue(exception)
146 rescues_path(rescue_templates[exception.class.name])
147 end
148
149 def response_code_for_rescue(exception)
150 rescue_responses[exception.class.name]
151 end
152
153 def clean_backtrace(exception)
154 if backtrace = exception.backtrace
155 if defined?(RAILS_ROOT)
156 backtrace.map { |line| line.sub RAILS_ROOT, '' }
157 else
158 backtrace
159 end
160 end
161 end
162 end
163 end