| 1 |
module ActionController |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 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) |
|---|
| 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) |
|---|
| 55 |
new.process(request, response, :rescue_action, exception) |
|---|
| 56 |
end |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 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 |
|
|---|
| 104 |
|
|---|
| 105 |
rescue_handlers << [key, options[:with]] |
|---|
| 106 |
end |
|---|
| 107 |
end |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
protected |
|---|
| 111 |
|
|---|
| 112 |
def rescue_action(exception) |
|---|
| 113 |
log_error(exception) if logger |
|---|
| 114 |
erase_results if performed? |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 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 |
|
|---|
| 130 |
def log_error(exception) |
|---|
| 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 |
|
|---|
| 145 |
|
|---|
| 146 |
def rescue_action_in_public(exception) |
|---|
| 147 |
render_optional_error_file response_code_for_rescue(exception) |
|---|
| 148 |
end |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 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 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
def local_request? |
|---|
| 168 |
request.remote_addr == LOCALHOST and request.remote_ip == LOCALHOST |
|---|
| 169 |
end |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 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 |
|
|---|
| 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 |
|---|
| 194 |
end |
|---|
| 195 |
end |
|---|
| 196 |
|
|---|
| 197 |
private |
|---|
| 198 |
def perform_action_with_rescue |
|---|
| 199 |
perform_action_without_rescue |
|---|
| 200 |
rescue Exception => exception |
|---|
| 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 |
|
|---|
| 220 |
|
|---|
| 221 |
_, handler = *rescue_handlers.reverse.detect do |klass_name, handler| |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 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 |
|---|