Skip to content

Instantly share code, notes, and snippets.

@wellington1993
Forked from metavida/dispatcher.rb.patch
Created November 28, 2018 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellington1993/b08cb862daefca5995a16edb129ac6b1 to your computer and use it in GitHub Desktop.
Save wellington1993/b08cb862daefca5995a16edb129ac6b1 to your computer and use it in GitHub Desktop.
A patch for a Mongrel + Rails 1.2.6 bug, described in detail here: http://billkirtley.wordpress.com/2009/03/03/failsafe-handling-with-rails/
--- a/rails-1.2.6/lib/dispatcher.rb
+++ b/rails-1.2.6/lib/dispatcher.rb
@@ -41,7 +41,7 @@
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(output, '500 Internal Server Error', exception, response) do
controller ||= (ApplicationController rescue ActionController::Base)
controller.process_with_exception(request, response, exception).out(output)
end
@@ -136,25 +136,38 @@
yield
rescue Exception # errors from executed block
begin
- output.write "Status: #{status}\r\n"
+ if response
+ response.headers['Status'] = status
+ else # we only have output, not a response instance
+ output.write "Status: #{status}\r\n"
+ end
if exception
message = exception.to_s + "\r\n" + exception.backtrace.join("\r\n")
- error_path = File.join(RAILS_ROOT, 'public', '500.html')
-
- if defined?(RAILS_DEFAULT_LOGGER) && !RAILS_DEFAULT_LOGGER.nil?
- RAILS_DEFAULT_LOGGER.fatal(message)
-
- output.write "Content-Type: text/html\r\n\r\n"
+ error_body = File.join(RAILS_ROOT, 'public', '500.html')
+ error_body = File.exists?(error_body) ?
+ IO.read(error_body) :
+ "<html><body><h1>Application error (Rails)</h1></body></html>"
+ has_logger = defined?(RAILS_DEFAULT_LOGGER) && !RAILS_DEFAULT_LOGGER.nil?
+
+ if response
+ if has_logger
+ RAILS_DEFAULT_LOGGER.fatal(message)
+ response.body = error_body
+ else
+ response.body = message
+ end
+ response.out output
- if File.exists?(error_path)
- output.write(IO.read(error_path))
+ else # we only have output, not a response instance
+ if has_logger
+ RAILS_DEFAULT_LOGGER.fatal(message)
+ output.write "Content-Type: text/html\r\n\r\n"
+ output.write error_body
else
- output.write("<html><body><h1>Application error (Rails)</h1></body></html>")
+ output.write "Content-Type: text/plain\r\n\r\n"
+ output.write message
end
- else
- output.write "Content-Type: text/plain\r\n\r\n"
- output.write(message)
end
end
rescue Exception # Logger or IO errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment