aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/failsafe.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-04-20 18:31:22 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-04-20 18:31:40 -0700
commit685a53a360d0d8fc4a9f3b49f900621c940a71f2 (patch)
treefa6b38f75a873a21c3e380f1acbab3d019293363 /actionpack/lib/action_dispatch/middleware/failsafe.rb
parentdb05c73fb6e2294c576ef9889c70940891682c32 (diff)
parent164a94d0bc8c9124ab820506e5ad79496395c026 (diff)
downloadrails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.tar.gz
rails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.tar.bz2
rails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.zip
Merge branch 'master' into cherry
Conflicts: activesupport/CHANGELOG activesupport/lib/active_support/core_ext/class/delegating_attributes.rb activesupport/lib/active_support/core_ext/hash/conversions.rb activesupport/lib/active_support/core_ext/module/attribute_accessors.rb activesupport/lib/active_support/core_ext/string/multibyte.rb activesupport/lib/active_support/core_ext/time/calculations.rb activesupport/lib/active_support/deprecation.rb
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/failsafe.rb')
-rw-r--r--actionpack/lib/action_dispatch/middleware/failsafe.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/failsafe.rb b/actionpack/lib/action_dispatch/middleware/failsafe.rb
new file mode 100644
index 0000000000..7379a696aa
--- /dev/null
+++ b/actionpack/lib/action_dispatch/middleware/failsafe.rb
@@ -0,0 +1,52 @@
+module ActionDispatch
+ 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["rack.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) && Rails.logger
+ Rails.logger
+ else
+ Logger.new($stderr)
+ end
+ end
+ end
+end