diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb index c121923da1..7ed3426832 100644 --- a/railties/lib/dispatcher.rb +++ b/railties/lib/dispatcher.rb @@ -39,7 +39,7 @@ class Dispatcher controller.process(request, response).out(output) end rescue Exception => exception # errors from CGI dispatch - failsafe_response(output, '500 Internal Server Error', exception) do + failsafe_response(cgi, output, '500 Internal Server Error', exception) do controller ||= ApplicationController rescue LoadError nil controller ||= ActionController::Base controller.process_with_exception(request, response, exception).out(output) @@ -90,12 +90,12 @@ class Dispatcher attr_accessor_with_default :preparation_callbacks, [] attr_accessor_with_default :preparation_callbacks_run, false alias_method :preparation_callbacks_run?, :preparation_callbacks_run - + # CGI.new plus exception handling. CGI#read_multipart raises EOFError # if body.empty? or body.size != Content-Length and raises ArgumentError # if Content-Length is non-integer. def new_cgi(output) - failsafe_response(output, '400 Bad Request') { CGI.new } + failsafe_response(nil, output, '400 Bad Request') { CGI.new } end def prepare_application @@ -131,34 +131,59 @@ class Dispatcher end # If the block raises, send status code as a last-ditch response. - def failsafe_response(output, status, exception = nil) + def failsafe_response(cgi, fallback_output, status, exception = nil) yield - rescue Exception # errors from executed block + rescue Exception begin - output.write "Status: #{status}\r\n" - - if exception - message = exception.to_s + "\r\n" + exception.backtrace.join("\r\n") - error_path = File.join(RAILS_ROOT, 'public', '500.html') + log_failsafe_exception(cgi, status, exception) - if defined?(RAILS_DEFAULT_LOGGER) && !RAILS_DEFAULT_LOGGER.nil? - RAILS_DEFAULT_LOGGER.fatal(message) + body = failsafe_response_body(status) + if cgi + head = { 'status' => status, 'type' => 'text/html' } - output.write "Content-Type: text/html\r\n\r\n" - - if File.exists?(error_path) - output.write(IO.read(error_path)) - else - output.write("