diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-12-04 20:39:36 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-12-04 20:39:36 -0600 |
commit | 9c9da6c892d715ca22c3cf51f50deb1d80029c66 (patch) | |
tree | 1605da4f53bb22b5f793c0d78161e46fdb8603bf /actionpack/lib/action_controller/failsafe.rb | |
parent | 27ebfd795ff106efae8cbe318d7b15b1a3c63b13 (diff) | |
download | rails-9c9da6c892d715ca22c3cf51f50deb1d80029c66.tar.gz rails-9c9da6c892d715ca22c3cf51f50deb1d80029c66.tar.bz2 rails-9c9da6c892d715ca22c3cf51f50deb1d80029c66.zip |
Boot out CGI Processor.
* Add ActionController::CGIHandler as a backwards compatible CGI wrapper around Rack.
* Also pull failsafe responder into ActionController::Failsafe middleware.
Diffstat (limited to 'actionpack/lib/action_controller/failsafe.rb')
-rw-r--r-- | actionpack/lib/action_controller/failsafe.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/failsafe.rb b/actionpack/lib/action_controller/failsafe.rb new file mode 100644 index 0000000000..bb6ef39470 --- /dev/null +++ b/actionpack/lib/action_controller/failsafe.rb @@ -0,0 +1,52 @@ +module ActionController + class Failsafe + cattr_accessor :error_file_path + self.error_file_path = Rails.public_path if defined?(Rails.public_path) + + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + rescue Exception => exception + # Reraise exception in test environment + if env["action_controller.test"] + raise exception + else + failsafe_response(exception) + end + end + + private + def failsafe_response(exception) + log_failsafe_exception(exception) + [500, {'Content-Type' => 'text/html'}, failsafe_response_body] + rescue Exception => failsafe_error # Logger or IO errors + $stderr.puts "Error during failsafe response: #{failsafe_error}" + end + + def failsafe_response_body + error_path = "#{self.class.error_file_path}/500.html" + if File.exist?(error_path) + File.read(error_path) + else + "<html><body><h1>500 Internal Server Error</h1></body></html>" + end + end + + def log_failsafe_exception(exception) + message = "/!\\ FAILSAFE /!\\ #{Time.now}\n Status: 500 Internal Server Error\n" + message << " #{exception}\n #{exception.backtrace.join("\n ")}" if exception + failsafe_logger.fatal(message) + end + + def failsafe_logger + if defined?(::RAILS_DEFAULT_LOGGER) && !::RAILS_DEFAULT_LOGGER.nil? + ::RAILS_DEFAULT_LOGGER + else + Logger.new($stderr) + end + end + end +end |