From deef8dd68241cdb9826a2293bced5e2257ccea90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 16 Dec 2011 09:29:37 +0100 Subject: Extract the rendering of public exceptions pages into a Rack app. --- .../action_dispatch/middleware/show_exceptions.rb | 51 ++++++++-------------- 1 file changed, 18 insertions(+), 33 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware/show_exceptions.rb') diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 0c95248611..4cfcb0a7db 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -6,8 +6,6 @@ module ActionDispatch # This middleware rescues any exception returned by the application # and wraps them in a format for the end user. class ShowExceptions - RESCUES_TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'templates') - FAILSAFE_RESPONSE = [500, {'Content-Type' => 'text/html'}, ["

500 Internal Server Error

" << "If you are the administrator of this website, then please read this web " << @@ -28,9 +26,19 @@ module ActionDispatch end end - def initialize(app, consider_all_requests_local = nil) - ActiveSupport::Deprecation.warn "Passing consider_all_requests_local option to ActionDispatch::ShowExceptions middleware no longer works" unless consider_all_requests_local.nil? + def initialize(app, exceptions_app = nil) + if [true, false].include?(exceptions_app) + ActiveSupport::Deprecation.warn "Passing consider_all_requests_local option to ActionDispatch::ShowExceptions middleware no longer works" + exceptions_app = nil + end + + if exceptions_app.nil? + raise ArgumentError, "You need to pass an exceptions_app when initializing ActionDispatch::ShowExceptions. " \ + "In case you want to render pages from a public path, you can use ActionDispatch::PublicExceptions.new('path/to/public')" + end + @app = app + @exceptions_app = exceptions_app end def call(env) @@ -40,7 +48,7 @@ module ActionDispatch raise exception if env['action_dispatch.show_exceptions'] == false end - response || render_exception_with_failsafe(env, exception) + response || render_exception(env, exception) end private @@ -50,37 +58,14 @@ module ActionDispatch def status_code(*) end - def render_exception_with_failsafe(env, exception) - render_exception(env, exception) + def render_exception(env, exception) + wrapper = ExceptionWrapper.new(env, exception) + env["action_dispatch.exception"] = wrapper.exception + env["PATH_INFO"] = "/#{wrapper.status_code}" + @exceptions_app.call(env) rescue Exception => failsafe_error $stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}" FAILSAFE_RESPONSE end - - def render_exception(env, exception) - wrapper = ExceptionWrapper.new(env, exception) - - status = wrapper.status_code - locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale - path = "#{public_path}/#{status}.html" - - if locale_path && File.exist?(locale_path) - render(status, File.read(locale_path)) - elsif File.exist?(path) - render(status, File.read(path)) - else - render(status, '') - end - end - - def render(status, body) - [status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]] - end - - # TODO: Make this a middleware initialization parameter once - # we removed the second option (which is deprecated) - def public_path - defined?(Rails.public_path) ? Rails.public_path : 'public_path' - end end end -- cgit v1.2.3